categories - why category__and and category__in wont work togather?

admin2025-06-03  4

I am having an issue here. I want to get all the post that are related to the current post at the same time it will also have a common category term.

for example:

Post XYZ has categories car, Truck, and Bus.

So i want to get any posts that have car or truck or bus at the same time it will also have category "Toronto"

What would be the query?

I am having an issue here. I want to get all the post that are related to the current post at the same time it will also have a common category term.

for example:

Post XYZ has categories car, Truck, and Bus.

So i want to get any posts that have car or truck or bus at the same time it will also have category "Toronto"

What would be the query?

Share Improve this question edited Feb 8, 2019 at 23:57 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Jan 12, 2014 at 0:32 Tpn KnvlTpn Knvl 32 bronze badges 2
  • 1 have a look at tax_query for complex taxonomy queries. – Milo Commented Jan 12, 2014 at 0:34
  • @Milo Perfect Solution. – Tpn Knvl Commented Jan 12, 2014 at 0:50
Add a comment  | 

1 Answer 1

Reset to default 1

You need a query like this -

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => array( 'car', 'truck', 'bus' ),
            'operator' => 'IN'
        ),
        array(
            'taxonomy' => 'category',
            'field' => 'name',
            'terms' => 'Toronto'
        )
    )
);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748928826a314901.html

最新回复(0)