Below is the current loop I have setup:

        //start products category loop
        $args = array(
            'hide_empty'  => 0,  
            'parent'      => 0,
            'orderby'     => 'name',
            'order'       => 'ASC',
            'taxonomy'    => 'profile_categories',
            'pad_counts'  => 1

        $categories = get_categories($args);
        foreach($categories as $category) { ?>
            // setup the category ID
            $cat_id = $category->term_id;
            // Get category name
            $cat_name = $category->name;
            // Get category count
            $cat_count = $category->count;

            //get the category url
            $cat_url = get_term_link( $category->slug, 'profile_categories' );

            echo "<h2>";
            echo $cat_name;
            $subcat_args = array(
                'hide_empty'  => 0,  
                'orderby'     => 'name',
                'child_of'    => $cat_id,
                'order'       => 'ASC',
                'taxonomy'    => 'profile_categories',
                'pad_counts'  => 1
            $subcategories = get_categories($subcat_args);
            $subcat_count = count($subcategories);
            if ($subcat_count > 0) { echo ' ('.$subcat_count.' Subcategories)'; }
            echo "</h2>";

            echo '<a href="';
            echo $cat_url;
            echo '">';
            echo __('View all','roots');
            echo "</a>";

            // Get posts belonging to current category
            $post_args = array(
                'posts_per_page' => 4,
                'post_type' => 'moulding_profiles',
                'tax_query' => array(
                        'taxonomy' => 'profile_categories',
                        'field' => 'id',
                        'parent' => 0,
                        'terms' => $cat_id
            $posts_query = new WP_Query($post_args);
            if ($posts_query->have_posts() ) {
                echo '<ul>';
                    while ($posts_query->have_posts()) : $posts_query->the_post(); ?>
                        <li><a href="<?php the_permalink();?>"><?php the_title(); ?></a> -> Width: <?php the_field('mp_profile_dimensions_width',get_the_ID()); ?> Height: <?php the_field('mp_profile_dimensions_height',get_the_ID()); ?></li>
                    <?php endwhile; 
                echo '</ul>';
            } // done our wordpress loop. Will start again for each category
            wp_reset_query(); // reset the query ?>
        <?php } // done the foreach statement ?>

What this does is list categories within a taxonomy called profile_categories, and then lists 4 items that are within each category. The category argument is using parent => 0 so that only top-level categories are shown. What I'd like to do is also use pad_counts => 1 to grab the count of not only the category, but all subcategories within that. So - if I'm thinking of this correctly, parents conflicts with pad_counts. I've looked through the codex about this and am stuck in the mud on if this is even possible - or if I'm that clueless and am missing something obvious. Any help would be greatly appreciated. Thanks!

Update: Using http://codex.wordpress.org/Function_Reference/get_categories as a reference, when parent is set to 0 (to only show top-level categories), the $category->count and $category->category_count does not include the count of the sub-categories - still thinking this is a bug unless someone knows of a workaround.


1 Answer 1


This is potentially a bug* - but it doesn't seem like it is the same one you have linked to. The problem lies in the get_terms function. Specifically here: http://core.trac.wordpress.org/browser/trunk/wp-includes/taxonomy.php#L1397

The array of terms (which when parent=0, are are just the top level terms) gets passed to _pad_term_counts which says:

Add count of children to parent count.

Recalculates term counts by including items from child terms. Assumes all relevant children are already in the $terms argument

So the _pad_term_counts function does its job properly, but its only ever given the top level terms and so only counts the posts in those terms, and not any child terms.

A work-around (this is not particular efficient), is to get all categories and then filter out all but the top level ones. Then has far as get_categories (and so get_terms) is concerned - you are after all terms and so all get counted:

    //The args. Don't set parent
    $args = array(
        'hide_empty'  => 0,  
        'orderby'     => 'name',
        'order'       => 'ASC',
        'taxonomy'    => 'category',
        'pad_counts'  => 1

    //I'll leave it to you to check for error objects etc.
    $categories = get_categories($args));
    $categories = wp_list_filter($categories,array('parent'=>0));

* Or maybe not: it is simply excluding the count of terms which do not match the criteria.

  • Awesome! Never knew about wp_list_filter() before. While not the most efficient, saves my sanity nonetheless. I can't give you the bounty as of yet (says I have to wait a little longer) but I'll jump back on shortly to try. Thanks again!
    – Zach
    Commented Apr 11, 2012 at 15:03
  • Great Answer. But a small code typo error on line get_categories($args)); It has an extra closing bracket. I tried to edit but it is a single line and the system doesn't let me do that. Thanks Commented Sep 11, 2014 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.