WP Query for Posts (Products) in Specific Category that has 2 Specific Tags (*AND* both tags not *OR*)

wordpress.stackexchange https://wordpress.stackexchange.com/questions/365887

Question

This is really confusing me.

I've tried numerous things but cannot get appropriate results.

Here's what I currently have for my query:

$args = array(
    'post_type' => 'product',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => array( 'ring' ),
        ),
        array(
            'taxonomy' => 'product_tag',
            'field'    => 'slug',
            'terms'    => array( 'black', 'men' ),
        ),
    ),
);
$query = new WP_Query( $args );

As you can see I am trying to return products in the product category ring that contain both the tags black AND men...

It appears though the product_tag terms array which includes black and men, is returning posts that include the tag black OR the tag men in the ring category.

How do I get it so that only products that include BOTH the tags in the query are returned?

Thanks so much

Was it helpful?

Solution

Nevermind I (think) I got it...

$args = array(
    'post_type' => 'product',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => array( 'ring' ),
        ),
        array(
            'taxonomy' => 'product_tag',
            'field'    => 'slug',
            'terms'    => array( 'black', 'men' ),
            'operator' => 'AND'
        ),
    ),
);

Missing the 'operator' => 'AND' key value pair

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top