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

时间:2013-12-05 22:07:30

标签: wordpress categories woocommerce

我正在使用WP_Query用于Woocommerce产品,以尝试查询特定类别的产品。这是对我有用的语法 -

$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来过滤,我想找到存在于多个类别 中的产品。

论证product_cat不是WP_Query的原生(至少我可以找到),所以我假设这是Woocommerce定制的东西。通过他们的文档,我找不到任何允许我按类别ID过滤的内容,也没有使用AND条件进行过滤。

使用cattax_querycategory__and的数组未产生任何结果。基本上,我想查询类别ID 102和115中存在的所有产品。如果我必须使用slug,我确信有一种方法可以根据我的ID获取该信息,但我会喜欢避免2个查询按多个类别进行过滤。

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

更新:我了解到在product_cat参数中用逗号分隔类别slugs会产生“OR”效果,所以它会合并两者的不同产品,但这不是我在找什么。所以,例如:

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

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

4 个答案:

答案 0 :(得分:28)

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

$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',以确保产品属于两种类别。

希望这可以帮助将来的某个人。

我也无法弄清楚如何传递一个ID,而不是一个slug(虽然我确定有一种方法),但这里是根据ID检索slug的功能:

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

答案 1 :(得分:11)

要按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
        )
    )
);

答案 2 :(得分:6)

来自WP_Query for Category Parameters上的WordPress codex:

相当于 OR

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

相当于 AND

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

e.g。

$query = new WP_Query( $args );

答案 3 :(得分:1)

在“tax_query”内部&#39;数组的数组你可以指定一个&#39;运算符&#39;要对查询执行。您可以使用&#39; AND&#39;来实现您想要的效果。操作

$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 );

此查询选择的所有产品都与提供的&#39;条款相匹配。有关详细信息,请参阅此链接:https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

如果链接中断,请输入相关信息:

  

operator(string) - 要测试的运算符。可能的值包括&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;并且“不存在”#39;默认值为&#39; IN&#39;。

相关问题