处理WP查询中隐藏产品的Woocommerce可见性

时间:2018-10-30 06:32:25

标签: php wordpress woocommerce product custom-taxonomy

您能帮我吗?我有一个产品列表,我想要隐藏在产品的可见性详细信息上标记为隐藏的那些产品。这是我的代码:

 $args = array(
                'posts_per_page' => -1,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'term_id',
                        // 'terms' => 'white-wines'
                        'terms' => $product_categories[$wc_arr_key[$wc_cat_id]]->term_id,
                        'visibility' => 'visible' //NOT WORKING...
                    )
                ),
                'post_type' => 'product',
                'orderby'    => 'menu_order',      
                'order'           =>  'ASC',      

            );
            $products = new WP_Query( $args );

            if(isset($_GET['staging']) && $_GET['staging'] == "true") {
                echo "<pre>" . print_r($products) . "</pre>";
            }

我要显示所有标记为可见的产品。

3 个答案:

答案 0 :(得分:1)

使用以下代码排除hidden个产品,并仅显示visible个产品

$args = array(
                'posts_per_page' => -1,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'term_id',
                        // 'terms' => 'white-wines'
                        'terms' => $product_categories[$wc_arr_key[$wc_cat_id]]->term_id
                    )
                ),
                'meta_query' => array(
                    array(
                       'key'       => '_visibility',
                       'value'     => 'hidden',
                       'compare'   => '!='
                         )
                 ),
                'post_type' => 'product',
                'orderby'    => 'menu_order',      
                'order'           =>  'ASC'      

            );
            $products = new WP_Query( $args );

            if(isset($_GET['staging']) && $_GET['staging'] == "true") {
                echo "<pre>" . print_r($products) . "</pre>";
            }

答案 1 :(得分:1)

WooCommerce将此数据另存为元数据,因此您需要针对名称_visibility运行元查询。看起来像

'meta_query' => array(
    array(
        'key'       => '_visibility',
        'value'     => 'hidden',
        'compare'   => '!=',
    )
)

这将拉出所有meta_visibility不等于隐藏的帖子

答案 2 :(得分:1)

自Woocommerce 3起,产品可视性由术语product_visibility的分类法exclude-from-catalog处理,因此您需要在税收查询数组中使用第二个数组:

$terms = array( $product_categories[$wc_arr_key[$wc_cat_id]]->term_id );

$products = new WP_Query( array(
    'post_type'         => 'product',
    'post_status'       => 'publish',
    'posts_per_page'    => -1,
    'tax_query'         => array(
        'relation'      => 'AND',
        array(
            'taxonomy'  => 'product_cat',
            'field'     => 'term_id',
            'terms'     => $terms
        ),
        array(
            'taxonomy'  => 'product_visibility',
            'terms'     => array('exclude-from-catalog'),
            'field'     => 'name',
            'operator'  => 'NOT IN',
        ),
    ),
    'orderby'           => 'menu_order',
    'order'             =>  'ASC',
) );


if(isset($_GET['staging']) && $_GET['staging'] == "true") {
    echo "<pre>" . print_r($products) . "</pre>";
}

经过测试可以正常工作。

相关:Database changes for products in woocommerce 3