total post count of a category and its child categories wordpress

Use following code to get total posts count of a category and all its child categories. To use this code you have to add following function inside your themes functions.php file

function wp_get_postcount($id)
//return count of post in category child of ID 15
$count = 0;
$taxonomy = 'category';
$args = array(
  'child_of' => $id,
$tax_terms = get_terms($taxonomy,$args);
foreach ($tax_terms as $tax_term) {
$count +=$tax_term->count;
return $count;

And yo use this code to display post count call this function anywhere inside your wordpress template files as

echo wp_get_postcount(5);
// To get post count of category having category id 5

This code snippet is useful to get total post count from category and its child categories.
Change 5 with your category id for which you want total post count.


Leave a Reply

Your email address will not be published. Required fields are marked *