On custom archive pages, I want to show subcategories that don't have their own posts but have sub-subcategories that do have posts.

And if a child category does not have posts, I want to hide its parent too.

Here is part of the code I'm using:

if(is_category() && $has_category_children){ //$has_category_children is true if the current category has subcategories.
    $args = array(
        'hide_empty' => 1,
        'hierarchical' => 0,
        'parent' => $term->term_id,
    $categories = get_categories($args);
    foreach ($categories as $cat) { 
        echo $cat->category_nicename;

What is the right approach to achieving this?

  • Your categories having 2 level hierarchy or multilevel hierarchy? Thanks:)
    – ksr89
    Jun 2, 2014 at 5:13
  • ^Multilevel hierarchy. Jun 2, 2014 at 6:41
  • I tried setting 'hierarchical'=>1....Codex says "When true, the results will include sub-categories that are empty, as long as those sub-categories have sub-categories that are not empty." But that doesn't seem to help either. Jun 2, 2014 at 6:42

1 Answer 1


I ended up ensuring that all parent and grandparent categories are selected for a particular article, instead of just the child category.

That solved my problem. Now, sub-categories that have sub-sub-categories with posts are not treated as "empty".

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.