[wp-hackers] advanced taxonomy queries

Otto otto at ottodestruct.com
Fri Dec 21 11:33:46 UTC 2012


Ignore the previous answers by Ian and Chip, I think they missed the
"AND" part of the question.

The correct answer is to add an operator to your array.

$myquery['tax_query'] = array(
    array(
        'taxonomy' => 'category',
        'terms' => array('foo', 'bar'),
        'field' => 'slug',
        'operator' => 'AND',
    ),
);

You may also want to add 'include_children' => false, if you don't
want the category searcher to pick up posts that are in child
categories of foo and bar as well.

Note that there is a shortcut version of this called category__and,
which accepts the ID values of the categories only.

-Otto



On Thu, Dec 20, 2012 at 10:04 PM, Haluk Karamete
<halukkaramete at gmail.com> wrote:
> In this query, we get the posts in the foo cat OR the bar cat
>
> $myquery['tax_query'] = array(
>     array(
>         'taxonomy' => 'category',
>         'terms' => array('foo', 'bar'),
>         'field' => 'slug',
>     ),
> );
> query_posts($myquery);
>
> how do we change in the above snippet it so that we get the posts that
> are in both categpories? That is both in the foo cat *AND* in the bar
> cat?
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list