scribu/wp-query-multiple-taxonomies — Issue #20: Looking to show the post count instead of +/-
Author: Jeradin
Created: 2012-03-01 19:09:42+00:00
State: open

I am trying to display the post count of the combined taxonomies as you drill down.

I found a function that will show count of posts related to multiple taxonomies but not got it working with your plugin yet. Curious if its something that could be added or some direction in how to get the comma separated taxonomies displayed to make the function work.

function wdd_in_category_count($catslugs = '', $display = true) {
global $wpdb;

$post_count = 0;
$slug_where = '';
$catslugs_arr = split(',', $catslugs);

foreach ($catslugs_arr as $catslugkey => $catslug) {
if ( $catslugkey > 0 ) {
$slug_where .= ', ';
}

$slug_where .= "'" . trim($catslug) . "'";
}

$slug_where = "cat_terms.slug IN (" . $slug_where . ")";

$sql =	"SELECT	COUNT( DISTINCT cat_posts.ID ) AS post_count " .
"FROM	" . $wpdb->term_taxonomy . " AS cat_term_taxonomy INNER JOIN " . $wpdb->terms . " AS cat_terms ON " .
"cat_term_taxonomy.term_id = cat_terms.term_id " .
"INNER JOIN " . $wpdb->term_relationships . " AS cat_term_relationships ON " .
"cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id " .
"INNER JOIN " . $wpdb->posts . " AS cat_posts ON " .
"cat_term_relationships.object_id = cat_posts.ID " .
"WHERE	cat_posts.post_status = 'publish' AND " .
"cat_posts.post_type = 'post' AND " .
"cat_term_taxonomy.taxonomy = 'category' AND " .
$slug_where;

$post_count = $wpdb->get_var($sql);

if ( $display ) {
echo $post_count;
}

return $post_count;

}