在Woocommerce的WP_query中获取目录中可见的产品 [英] Get products which are visible in catalog in a WP_query on Woocommerce
本文介绍了在Woocommerce的WP_query中获取目录中可见的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在尝试将所有产品归入目录中可见的类别页面。我在这里尝试过此方法,但此查询没有产品:
I'm currently trying to get all products in a category page which are visible in catalog. I've tried this here but I'm getting no products with this query:
$args = array(
'post_type' => 'product',
'product_cat' => get_queried_object()->slug,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array( 'catalog', 'visible' ),
'compare' => 'IN',
)
)
);
$loop = new WP_Query( $args );
var_dump( $loop );
当我删除 meta_query
可见性部分时,正在获得所有产品,包括隐藏的产品,但我只需要可见的产品。
When I remove the meta_query
visibility part I'm getting all products including the hidden ones but I just need the visible ones. Whats wrong here?
推荐答案
这需要是一个税收查询,而不是(因为woocommerce 3现在由woocommerce 3处理) product_visibility
自定义分类):
This need to be a tax query instead (as since woocommerce 3 it is now handled by product_visibility
custom taxonomy):
$loop = new WP_Query(array(
'post_type' => 'product',
'product_cat' => get_queried_object()->slug,
'tax_query' => array( array(
'taxonomy' => 'product_visibility',
'terms' => array( 'exclude-from-catalog' ),
'field' => 'name',
'operator' => 'NOT IN',
) )
) );
var_dump( $loop );
现在应该可以更好地工作了。
This should better work now.
这篇关于在Woocommerce的WP_query中获取目录中可见的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文