Display Groups of Custom Posts by their Custom Taxonomy Term
-
Hi,
I’m trying to output a portfolio page composed of sections of CPT post thumbnails organized by their custom taxonomy term slugs.
While it’s easy for me to output the terms associated with the custom taxonomy, I’m not sure how one nests a custom query to retrieve the posts associated with each term in the foreach loop.
I’ve included the code that I’ve worked on; but this is a bit beyond my PHP skill level.
//retrieves an array of the terms as slugs $terms = get_terms('tn_cstm_work_taxonomy', 'fields=names'); // This returns all posts in the custom taxonomy, rather than posts for each term $projectsArgs = array( 'tax_query' => array(array( 'taxonomy' => 'tn_cstm_work_taxonomy', 'field' => 'slug', 'terms' => $terms )) ); foreach ( $terms as $term ) { ?> <div class="row"> //Populates the the section and titles with the slug; needed for navigation <section id="<?php echo $term; ?>" class="large-12 columns" data-magellan-destination='<?php echo $term; ?>'> <h3><?php echo $term; ?></h3> <ul class="large-block-grid-4 small-block-grid-2"> <?php $projects = new WP_Query($slug); while ($projects->have_posts()) : $projects->the_post(); ?> <li> <?php //outputs thumbnails for gallery if(function_exists('tn_cstm_work_thumb')) { tn_cstm_work_thumb(); ?> </li> <?php endwhile; wp_reset_postdata(); ?> </ul> </section> </div><!-- .row --> <?php } ?>
This is obviously incorrect, the custom query will just pull all of the posts associated with any term in the custom taxonomy. Is there a way to output just the posts associated with each term?
Any hints, or help would be appreciated.
Thanks!
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Display Groups of Custom Posts by their Custom Taxonomy Term’ is closed to new replies.