[wp-hackers] advanced taxonomy queries

Haluk Karamete halukkaramete at gmail.com
Fri Dec 21 20:37:37 UTC 2012


Thank you all.

Otto, you must have recognized the original foo/bar snippet. That was
from your page.
It might be a good idea to add the operator option "ADD" for the rest
of us - somewhere around where you just talk about the "NOT IN"
operator option.




On Fri, Dec 21, 2012 at 4:28 AM, Chip Bennett <chip at chipbennett.net> wrote:
> You're right, Otto. That's what I get for answering questions at 11:30 at
> night. :)
>
> Just change my answer to use 'category__and', instead of 'category__in'.
> The funny thing is, I almost added that as an addendum to say,
> "category__in will give you foo OR bar, and category__and will give you foo
> AND bar."
>
> Chip
>
>
> On Fri, Dec 21, 2012 at 6:33 AM, Otto <otto at ottodestruct.com> wrote:
>
>> 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
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> 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