custom taxonomy - Listing posts under primary and secondary taxonomies

admin2025-06-04  1

Following on from this post:

List all posts in custom post type by taxonomy

How would I go about doing the same as above with a Custom Post Type called Area, but looping through the child-categories to get something like:

  • Primary Area
    • Post
    • Post
    • Secondary/Child Area
      • Post
      • Post
      • Post

Following on from this post:

List all posts in custom post type by taxonomy

How would I go about doing the same as above with a Custom Post Type called Area, but looping through the child-categories to get something like:

  • Primary Area
    • Post
    • Post
    • Secondary/Child Area
      • Post
      • Post
      • Post
Share Improve this question asked Jan 17, 2019 at 15:21 SketchybearSketchybear 233 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This only takes care of two levels. You could make a recursive function, if you have 'unlimited' levels.

$custom_terms = get_terms('custom_taxonomy');

/**
 * Loop through all taxonomies
 */
foreach($custom_terms as $custom_term):

    // Reset query (for good measures sake)
    wp_reset_query();

    // Set the args (getting ready to get posts that are in the given taxonomy)
    $outer_args = array(
        'post_type' => 'custom_post_type',
        'tax_query' => array(
            array(
                'taxonomy' => 'custom_taxonomy',
                'field' => 'slug',
                'terms' => $custom_term->slug,
                'post_parent' => 0   // This means that it's only top-level taxonomies included
            )
          )
       );

    // Get the information for the outer loop.
    $outer_loop = new WP_Query( $outer_args );

    if($outer_loop->have_posts()):
      echo '<h2>'.$custom_term->name.'</h2>';

      // Loop through outer loop    
      while( $outer_loop->have_posts() ): 
        $outer_loop->the_post(); 
        $outer_loop_ID = get_the_ID();

        // Display OUTER loop info: 
        echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';


        /**
         * Inner loop
         */
        wp_reset_query();
        $inner_args = array('post_type' => 'custom_post_type',
            'tax_query' => array(
                array(
                  'taxonomy' => 'custom_taxonomy',
                  'field' => 'slug',
                  'terms' => $custom_term->slug,
                  'post_parent' => $outer_loop_ID   // This gets the posts that has the outer loops post as parent
              )
           )
        );

        $inner_loop = new WP_Query($inner_args);
        if($inner_loop->have_posts()):

          // Display inner loop information
          echo '<h2>'.$custom_term->name.'</h2>';
          while($inner_loop->have_posts()) : 
            $inner_loop->the_post();

             // Display INNER loop info: 
             echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';

          endwhile; // Inner loop 

        endif; // if($inner_loop->have_posts()) {

      endwhile; // Outer loop

   endif; // if($outer_loop->have_posts()):

endforeach; // foreach($custom_terms as $custom_term):
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749001874a315520.html

最新回复(0)