Checking Daniel Llewellyn code and WordPress’ WP_Query reference, I realized that we need to pass an array of array within the tax_query parameter. Otherwise, it’d just return all post counts.
Here is the little modified code:
if ( ! function_exists( 'ipt_kb_total_cat_post_count' ) ) :
/**
* Simple function to get category post count including all subcategories
*
* @link http://wordpress.stackexchange.com/a/91551 Stackexchange
* @param int $cat_id Category ID
* @return int Total post count
*/
function ipt_kb_total_cat_post_count( $cat_id ) {
$q = new WP_Query( array(
'nopaging' => true,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $cat_id,
'include_children' => true,
),
),
'fields' => 'ids',
) );
return $q->post_count;
}
endif;
Hope it helps someone like me.