WP_Query Woocommerce 产品只属于不同的多个类别 tax_query [英] WP_Query Woocommerce products that belong in distinct multiple categories only tax_query
问题描述
我将 WP_Query
用于 Woocommerce 产品,以尝试查询特定类别中的产品.这是对我有用的语法 -
I'm using WP_Query
for Woocommerce products in attempt to query products in a particular category. This is the syntax that worked for me -
$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();
这将返回数据,但我想传递一个 ID,而不是类别 slug,以进行过滤,并且我想查找仅存在于多个类别中的产品.
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.
参数 product_cat
不是 WP_Query
原生的(至少我能找到),所以我假设这是 Woocommerce 的自定义内容.通过他们的文档,我找不到任何可以让我按类别 ID 进行过滤的内容,也无法使用 AND 条件进行此过滤.
The argument product_cat
is 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.
使用cat
,tax_query
和category__and
的数组没有产生任何结果.本质上,我想查询类别 ID 102 和 115 中存在的所有产品.如果我必须使用 slug,我确定有办法根据我拥有的 ID 获取该信息,但我会喜欢避免 2 个查询按多个类别过滤.
Using cat
, the array of tax_query
, and category__and
have 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.
有谁知道如何做到这一点?
Does anyone know how to accomplish this?
更新:我了解到,在 product_cat
参数中用逗号分隔类别 slug 会产生或"效果,因此它将合并来自两者的不同产品,但是这不是我要找的.因此,例如:
UPDATE: I have learned that separating category slugs by commas in the product_cat
argument 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' => '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.
推荐答案
哇,经过几个小时的敲打,我终于解决了这个问题 -
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 );
这利用了 tax_query
参数,包括 relation =>;'AND'
以确保产品属于 BOTH 类别.
This takes advantage of the tax_query
argument, including the relation => 'AND'
to make sure the product falls under BOTH categories.
希望这对未来的人有所帮助.
Hope this helps someone in the future.
我也无法弄清楚如何传递 ID 而不是 slug(虽然我确定有一种方法),但这里是基于 ID 检索 slug 的函数:
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:
$terms = get_term($YOURID, 'product_cat');
$theslug = $terms->slug;
这篇关于WP_Query Woocommerce 产品只属于不同的多个类别 tax_query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!