Custom loop with multiple taxonomy queries
What I Have: I’ve got the code for a working events list. There’s a single custom post (events) and three taxonomies (location, month, type). The following code lists future events and orders them by a custom field (start_date). The events list is also sortable by taxonomy. For instance, you can look at just events in December (month) or for events by location (Montana).
What I Can’t Figure Out: How to sort lists by two different taxonomies. For instance: Events in December (month) that are also in Montana (location). It just seems to pick one or the other.
Other Info: When using the standard WordPress loop, not the custom one below, there’s no problem with multiple queries with this CPT and taxonomies, but of course, it posts every event that ever happened, including the ones in the past.
Also, this is my first wp_query, so I’m hoping it’s something ovbious.
<?php $today = date('Ymd'); $my_query = new WP_Query( array( 'post_type' => 'events', 'tax_query' => array( 'relation' => 'OR', array( 'taxonomy' => 'location', 'terms' => $term, ), array( 'taxonomy' => 'month', 'terms' => $term, ), array( 'taxonomy' => 'type', 'terms' => $term, )), 'posts_per_page' => 10, 'meta_compare' => '>', 'meta_value' => $today, 'orderby' => 'meta_value', 'meta_key' => 'start_date', 'order' => 'ASC', 'paged'=> $paged, )); ?> <?php if ( $my_query->have_posts() ) : while ($my_query->have_posts() ) : $my_query->the_post();?> <!-- OUTPUT OF QUERY --> <?php endwhile; wp_reset_postdata(); endif; ?>
- The topic ‘Custom loop with multiple taxonomy queries’ is closed to new replies.