Custom Loop – Custom Post Type Sorted By Custom Taxonomy
-
<div class="content"> <?php $categories = get_terms('series', 'orderby=title&order=ASC&hide_empty=1'); foreach( $categories as $category ): ?> <h3><?php echo $category->name; ?></h3> <?php $posts = get_posts(array( 'post_type' => 'courses', 'order' => 'ASC', 'orderby' => 'title', 'taxonomy' => $category->taxonomy, 'term' => $category->slug, 'nopaging' => true, 'tax_query' => array( array( 'taxonomy' => 'role', 'field' => 'slug', 'terms' => 'developer' ) ) )); foreach($posts as $post): setup_postdata($post); ?> <?php get_template_part( 'courselisting', 'index' ); ?> <?php endforeach; ?> <?php endforeach; ?> </div>Training website. Custom post type: courses Custom taxonomies: series, roles
I have a bunch of courses, grouped into series. Using the code above, I’m producing a nice list of courses, grouped by series, for only role=developer.Problem is, it’s listing every single ‘series’ taxonomy – even if there are no role=developer courses for that series. (so i’m getting just a blank <h3>Series Name</h3> and then no course records after it.
I see I need to somehow limit the get_terms for series to only produce terms if they are present for courses where role=developer is true.Any help? I’m a bit stumped. Thanks!
The topic ‘Custom Loop – Custom Post Type Sorted By Custom Taxonomy’ is closed to new replies.