• Hello,

    I’m trying to display a integer with total post counts of a specific category including all its child categories (over one hundred).

    wp count posts doesn’t work for me because it counts everything, while I only need a precise category and all its child categories

    I’ve tried with wp list categories, but it returns also an array with category names; I don’t need the category names, only the total count of posts and it must be a sum of all posts from these different categories

    I haven’t found any plugin suitable for my goal

    how to achieve what I want?

    thanks for the help

Viewing 3 replies - 1 through 3 (of 3 total)
  • Didn’t test much but try:

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

    Thread Starter Il Gatto


    wonderful Michael! It works like a charm

    there’s only a minor quirk to solve now….

    some posts are counted twice because they’re two level deep

    Category 1
    |-Category 2 (child of 1)
    –|–Category 3 (child of 2)

    using your code if we count all posts in category 1 and there’s a post in category 3, that post is counted twice because it shows up also as a post in category 2

    I’ve currently 2 dummy posts in the theme I’m preparing but your code shows a count of 4 because of the reason explained above

    thanks a lot though 🙂 this is very close to what I was looking for

    Thread Starter Il Gatto



Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to display total post count of a category and all its child categories?’ is closed to new replies.