Last active
August 29, 2015 14:01
-
-
Save crondeau/4ff8d4ede5306a7e8190 to your computer and use it in GitHub Desktop.
Display list of CPT per taxonomy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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