I have a client with 4 levels of categories. They would like to have a page that is grouped by subcategory levels. There are posts in all categories at some level. That is, if there are no posts at the 2nd or 3rd level, there are at the 4th level.

What I've tried

The first two levels of WP_Query return the required fields from both category and posts. The 3rd level query object does not.

I've tried wp_reset_data() before the query.

I've adding suppress_filters' => true to the new query

if ( $SubSubSubCategories ) : 
  foreach( $SubSubSubCategories as $SubSubSubCategory ) :
    echo 'Category: '.$SubSubSubCategory->term_id;
    echo '<div class="CategorySubtitle">'.$SubSubSubCategory->name.'</div>
    <!--end Subtitle-->';

    $SubSubSubCategoryQuery1 = new WP_Query( array(
              'category__in' => array( $SubSubSubCategory->term_id ),
              'posts_per_page' => -1, 
              'suppress_filters' => true
    ) );

    echo 'Post count: '.$SubSubSubCategoryQuery1->post_count;

The first two echos in the code return the expected values. The post_count returns 0 in all cases. https://goosebaylumber.net/wordpress/category/wood-surfboards


1 Answer 1


The issue turned out to be custom post types. Even though a custom post type produces posts, WP_Query() doesn't select them unless specifically coded to do so with 'post_type' => 'slug', in the query arguments.

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.