Creating multilevel wp taxonomy query

Im creating a document library with a custom post type of ‘downloads’ and custom taxonomy for categories of ‘downloadCategory’.
Below is what i have so far which works fine and then i have a taxonomy template set up to handle the individual download view.

However this doesnt seem to handle multiple levels of categories, as i have created a child category and its still appearing in my main loop below.

I ideally want it so that it shows the top level categories first, then any child categories or posts when you click into it.

How do i go about setting this up so it handles multiple levels?

    $tax = 'downloadCategory';

    $terms = get_terms( $tax, [
      'hide_empty' => true,


    foreach( $terms as $term ) {

      if(  $term->count > 0 ) {  ?>

          <div class="category">
            <h4><a href="<?php echo get_term_link( $term );?>"><?php echo $term->name; ?></a></h4>
            <p><?php echo $term->description; ?></p>
            <a class="btn btn-outline btn-primary" href="<?php echo get_term_link( $term );?>">View</a>

  <?php }
} ?>

Read more here: Creating multilevel wp taxonomy query

Leave a Reply

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