tax_query operator woes

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’.

Read More

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?

Related posts

Leave a Reply

1 comment

  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);