最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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

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

Share Improve this question asked May 5, 2020 at 12:25 bbrumanbbruman 4841 gold badge5 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论