$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom taxonomy - Listing posts under primary and secondary taxonomies|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom taxonomy - Listing posts under primary and secondary taxonomies

matteradmin7PV0评论

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):
Post a comment

comment list (0)

  1. No comments so far