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.

I am trying to filter posts by multiple taxonomies. My code below works great, but their is one thing I can't quite figure out. When I filter the posts it seems that they only show the posts that apply to all my taxonomy 'terms'.

So to illustrate my problem: Let's say I have a post and that post is "Montana". And Montana has a 'regions' taxonomy of 'region1'. It also has a 'population' of 'pop1'. And we have another post called "Idaho" and is in 'region2' and is in 'pop1'and, additionally, it is also in 'pop2'.

So I run my query:

$myquery['tax_query'] = array(
'relation' => 'OR',
array(

       'taxonomy' => 'regions',

       'terms' => array(region1),

       'field' => 'slug',

       'operator' => 'IN',
     ),
array(

       'taxonomy' => 'population',

       'terms' => array(pop1),

       'field' => 'slug',

       'operator' => 'IN',

)
);

query_posts($myquery);

Everything works great...Idaho and Montana Both show up. So I run another query, this time filter by another population term 'pop2' along with 'pop1'.

$myquery['tax_query'] = array(
'relation' => 'OR',
array(

       'taxonomy' => 'regions',

       'terms' => array(region1),

       'field' => 'slug',

       'operator' => 'IN',
     ),
array(

       'taxonomy' => 'population',

       'terms' => array(pop1,pop2),

       'field' => 'slug',

       'operator' => 'IN',

)
);

query_posts($myquery);

Now Idaho and Montana should both show up right?...but they don't. Only Montana shows up because it has 'region1', but Idaho doesn't show up because it isn't in region1 nor does it have BOTH 'pop1' and 'pop2'.

So my question:

Is it possible to have the taxonomy operator work when at least one of the terms match instead of all of them having to be true?

share|improve this question
    
No, it should work. "IN" is equivalent to an "OR". It doesn't have to have both pop1 and pop2. The resulting SQL would be "population IN (pop1, pop2)". Which is basically an either or choice there. –  Otto Oct 23 '11 at 22:41
add comment

1 Answer 1

I'm not sure that there is a way to get it to use 'OR' instead of 'AND'. Alternatively, you can do:

$myquery['tax_query'] = array(
    'relation' => 'OR',
    array(
        'taxonomy' => 'regions',
        'terms' => array('region1'),
        'field' => 'slug',
        'operator' => 'IN'
    ),
    array(
        'taxonomy' => 'population',
        'terms' => array('pop1'),
        'field' => 'slug',
        'operator' => 'IN'
    ),
    array(
        'taxonomy' => 'population',
        'terms' => array('pop2'),
        'field' => 'slug',
        'operator' => 'IN'
    )
);

query_posts($myquery);
share|improve this answer
    
Ok...that's what I was trying to avoid. Hopefully they come up with an 'OR' in the near future. Thanks for your help, I appreciate it. –  kramdraw85 Sep 13 '11 at 17:09
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.