简体   繁体   English

获取Woocommerce上WP_query目录中可见的产品

[英]Get products which are visible in catalog in a WP_query on Woocommerce

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

When I remove the meta_query visibility part I'm getting all products including the hidden ones but I just need the visible ones. 当我删除meta_query可见性部分时,我得到的所有产品包括隐藏的产品,但我只需要可见的产品。 Whats wrong here? 这里有什么不对?

This need to be a tax query instead (as since woocommerce 3 it is now handled by product_visibility custom taxonomy) : 这需要是税务查询(因为woocommerce 3现在由product_visibility自定义分类法处理)

$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. 这应该更好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM