Support » How-To and Troubleshooting » Custom loop with multiple taxonomy queries

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.

    File: (archive-events.php)

    $today = date('Ymd');
    $my_query = new WP_Query( array(
        'post_type' => 'events',
    		'tax_query' => array(
    		'relation' => 'OR',
    			'taxonomy' => 'location',
    			'terms' => $term,
    			'taxonomy' => 'month',
    			'terms' => $term,
    			'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;
    endif; ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Have you tried setting the ‘relation’ to ‘AND’?

    ‘relation’ => ‘AND’,

    Just tried it. It returned zero results.

    You are supplying the same variable ($term) to all of the taxonomies. It should be different for each one.

    Actually, the following code helped me get where I was going…

    $today = date('Ymd');
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts($query_string . '&showposts=30&meta_key=calendar_date&meta_compare=>&orderby=meta_value&order=ASC&meta_value=' . $today);
    <?php if (have_posts() ) : while (have_posts() ) : the_post();?>
    <!-- OUTPUT OF QUERY -->
    <?php endwhile; else: ?>
    No events were found. Sorry!
    <?php wp_reset_query(); endif;?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom loop with multiple taxonomy queries’ is closed to new replies.
Skip to toolbar