wordpress WP_Query Woocommerce 产品只属于不同的多个类别 tax_query

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20411807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:25:51  来源:igfitidea点击:

WP_Query Woocommerce products that belong in distinct multiple categories only tax_query

wordpresscategorieswoocommerce

提问by RCNeil

I'm using WP_Queryfor Woocommerce products in attempt to query products in a particular category. This is the syntax that worked for me -

我正在使用WP_QueryWoocommerce 产品来尝试查询特定类别中的产品。这是对我有用的语法 -

$args = array(
    'posts_per_page' => -1,
    'product_cat' => 'category-slug-here',
    'post_type' => 'product',
    'orderby' => 'title',
);
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '' . get_the_title() . '<br /><br />';
}
wp_reset_postdata();

This returns data, but I want to pass an ID, not a category slug, to filter and I want to find products that exist in multiple categories only.

这将返回的数据,但我想传递一个ID,而不是一个类别蛞蝓,过滤和我想找到存在于多个类别的产品

The argument product_catis not native to WP_Query(at least that I can find), so I'm assuming this is something custom to Woocommerce. Through their documentation, I haven't been able to find anything that will allow me to filter by category ID, nor use an AND condition for this filtering.

这个论点product_cat不是原生的WP_Query(至少我能找到),所以我假设这是 Woocommerce 的自定义内容。通过他们的文档,我找不到任何可以让我按类别 ID 进行过滤的内容,也无法使用 AND 条件进行此过滤。

Using cat, the array of tax_query, and category__andhave not yielded any results. Essentially, I would like to query all products that exist in both category ID 102, and 115. If I have to use slugs, I'm sure there is a way around getting that info based on the ID I have, but I'd like to avoid 2 queries to filter by multiple categories.

使用cat、 的数组tax_querycategory__and没有产生任何结果。本质上,我想查询类别 ID 102 和 115 中存在的所有产品。如果我必须使用 slug,我确定有办法根据我拥有的 ID 获取该信息,但我会喜欢避免 2 个查询按多个类别过滤。

Does anyone know how to accomplish this?

有谁知道如何做到这一点?

UPDATE:I have learned that separating category slugs by commas in the product_catargument will produce an "OR" effect, so it will combine distinct products from both, but this is not what I am looking for. So, for example:

更新:我了解到在product_cat参数中用逗号分隔类别 slug会产生“或”效果,因此它将结合来自两者的不同产品,但这不是我正在寻找的。因此,例如:

 'product_cat' => 'category-slug1, category-slug2'

will return products from both categories in total, but I am still searching for a way to find distinct products that ONLY belong to both, or multiple, categories.

将总共​​返回两个类别的产品,但我仍在寻找一种方法来查找仅属于两个或多个类别的不同产品。

回答by RCNeil

Wow, so after hours of banging my head, this is how I was able to solve this -

哇,所以经过几个小时的敲击我的头,这就是我能够解决这个问题的方式 -

$args = array(
    'posts_per_page' => -1,
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'category-slug1'
        ),
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'category-slug2'
        )
    ),
    'post_type' => 'product',
    'orderby' => 'title',
);
$the_query = new WP_Query( $args );

This takes advantage of the tax_queryargument, including the relation => 'AND'to make sure the product falls under BOTH categories.

这利用了这个tax_query论点,包括relation => 'AND'确保产品属于 BOTH 类别。

Hope this helps someone in the future.

希望这对未来的人有所帮助。

I was also not able to figure out how to pass an ID, rather than a slug (although I'm sure there's a way), but here's the function to retrieve the slug based on an ID:

我也无法弄清楚如何传递 ID,而不是 slug(虽然我确定有办法),但这里是基于 ID 检索 slug 的函数:

$terms = get_term($YOURID, 'product_cat'); 
$theslug = $terms->slug; 

回答by user2718602

To query by category_ID this is what worked for me.

要按 category_ID 查询,这对我有用。

// First obtain term id:
//...
$all_categories = get_categories( $args );
$cat_ids = array();

foreach ($all_categories as $cat) 
{
     array_push($cat_ids, $cat->term_id);
}

//Now use ids from array:
$args = array(
    'posts_per_page' => -1,
    'post_type' => 'product',
    'tax_query'     => array(
        array(
            'taxonomy'  => 'product_cat',
            'field'     => 'id', 
            'terms'     => $cat_ids
        )
    )
);

回答by Daniel Twork

From the WordPress codex on WP_Queryfor Category Parameters:

从 WordPress codex on WP_Queryfor Category Parameters

equivalent of OR

相当于OR

$args = array( 'product_cat' => 'category-slug1,category-slug2' ) );

equivalent of AND

相当于AND

$args = array( 'product_cat' => 'category-slug1+category-slug2' );

e.g.

例如

$query = new WP_Query( $args );

回答by asdf

Inside a 'tax_query' array's array you can specify an 'operator' to be performed on the query. You can achieve what you want using the 'AND' operator.

在“tax_query”数组的数组中,您可以指定要对查询执行的“运算符”。您可以使用“AND”运算符来实现您想要的。

$args = array(
'posts_per_page' => -1,
'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'product_cat',
        'field' => 'slug',
        'terms' => array( 'category-slug1', 'category-slug2' )
        'operator => 'AND',
    ),
),
'post_type' => 'product',
'orderby' => 'title',
);
$the_query = new WP_Query( $args );

All of the products selected by this query will match the provided 'terms'. See this link for more info: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

此查询选择的所有产品都将匹配提供的“条款”。有关更多信息,请参阅此链接:https: //codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

In case the link ever breaks, here's the relevant info:

如果链接中断,以下是相关信息:

operator (string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND', 'EXISTS' and 'NOT EXISTS'. Default value is 'IN'.

operator (string) - 要测试的运算符。可能的值为“IN”、“NOT IN”、“AND”、“EXISTS”和“NOT EXISTS”。默认值为“IN”。