Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Apologies -- new to the stackexchange and actually posted this over in Stack Overflow because I didn't know there was a WP specific joint.

My issue--

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 seem 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 obvious.

File: (archives-events.php)

<?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; ?>
share|improve this question
add comment

1 Answer

Here's a function to do a query with multiple taxonomies:

function posts_search ($post_type,$taxonomies) { // $taxonomies should be an array ('taxonomy'=>'term', 'taxonomy2'=>'term2')
    foreach ($taxonomies as $key=>$value) {
        $args=array('post_type'=>$post_type,'post__in'=>$ids,$key=>$value);
        unset($ids); $ids=array();
        foreach($posts=get_posts($args) as $post) { $ids[]=$post->ID; }
        if (empty($ids)) return false;
    }
    return $posts;
}

And here's an example on how i used it in a past project:

$posts = posts_search ('produtos',array('prod-categoria'=>'blocos','prod-cols'=>'7-c')); 
if($posts) { 
    foreach($posts as $post) { 
        // show infos from the post... 
    } 
}

The produtos is the custom post type upon which i'm doing the search, prod-categoria and prod-cols are two custom taxonomies and blocos and 7-c are two terms from the previous custom taxonomies.

Hope that helps.

OBS1; the function is not from me, but probably from an old post here. OBS2; this was funcional on a 3.1.2 installation.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.