Display all categories and their respective parents and children

So I have some post types that are assigned a category, which have a parent category. How would I go about displaying the parent category in a list along with its child and post? Something like this:

Parent Cat

-> Child Cat

-> -> Post

This is what I have written, but it displays ALL of the categories and not the associated posts.

 <?php

    $args = array(
        'post_type' => 'job_listing'
    );
    $categories = get_categories( $args );
    $posts = get_posts($args);

    foreach ( $categories as $category ) {
        echo '<h4>' . $category->name . '</h4>';
     ?>

        <div class="menu-items-container">
            <?php foreach($posts as $post) { ?>
                <?php if (in_category($category)) { ?>
                    <p><?php the_post_title(); ?></p>
                <?php  } ?>
            <?php  } ?>
        </div>
<?php } ?>

>Solution :

To display the categories and posts in a parent-child hierarchy, you need to use the get_terms() function instead of get_categories(), as it allows you to specify the taxonomy (category or custom taxonomy) you want to retrieve terms from. Then, you can use a nested loop to display the parent and child categories, and the posts that belong to them.

Here’s an example implementation in WordPress PHP:

<?PHP
$taxonomy = 'category'; // change this to the name of your custom taxonomy if 
applicable
$terms = get_terms(array(
    'taxonomy' => $taxonomy,
    'hide_empty' => false,
    'parent' => 0 // only retrieve top-level terms (i.e. parent categories)
));
foreach ($terms as $parent_term) {
    echo '<h4>' . $parent_term->name . '</h4>';
    $child_terms = get_terms(array(
         'taxonomy' => $taxonomy,
         'hide_empty' => false,
         'parent' => $parent_term->term_id // retrieve child terms of the current parent category
     ));
     foreach ($child_terms as $child_term) {
         echo '<p>' . $child_term->name . '</p>';
         $posts = get_posts(array(
             'post_type' => 'job_listing',
             'tax_query' => array(
                array(
                     'taxonomy' => $taxonomy,
                     'field' => 'term_id',
                     'terms' => $child_term->term_id // retrieve posts that belong to the current child category
                 )
             )
         ));
         foreach ($posts as $post) {
             echo '<p>' . get_the_title($post->ID) . '</p>';
         }
    }
 }

In this code, we first retrieve the top-level terms (parent categories) using get_terms(), with a parent argument of 0. Then, we loop through the parent terms and display their names.

Inside the parent category loop, we use get_terms() again to retrieve the child terms (categories) of the current parent category, using the parent term ID as the parent argument. We then loop through the child terms and display their names.

Inside the child category loop, we use get_posts() to retrieve the posts that belong to the current child category, using a tax_query argument to filter by the child term ID. We then loop through the posts and display their titles.

Leave a Reply