Skip to content

Instantly share code, notes, and snippets.

@crondeau
Last active August 29, 2015 14:01
Show Gist options
  • Save crondeau/4ff8d4ede5306a7e8190 to your computer and use it in GitHub Desktop.
Save crondeau/4ff8d4ede5306a7e8190 to your computer and use it in GitHub Desktop.
Display list of CPT per taxonomy
<?php
$terms = get_terms('category'); //use category or your taxonomy
$count = count($terms);
//check if there are any terms
if ( $count > 0 ){
//loop through each term and query posts.
foreach($terms as $term){
$term_args = array(
'post_type' => 'resource', // this is the name of your cpt
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term->slug
)
),
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title' //or order by menu_order
);
$term_posts = new WP_Query($term_args);
if($term_posts->have_posts()):
echo '<h2>' . $term->name . '</h2>';
while($term_posts->have_posts()): $term_posts->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile; wp_reset_postdata();
endif;
}
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment