自 WooCommerce 3 起,有 2 种方法可以排除您的“缺货”产品WP_Query
:
1) 包括税务查询 like:
$products = new WP_Query( array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'posts_per_page' => 6,
'orderby' =>'meta_value_num',
'order' => 'DESC',
'tax_query' => array( array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => array('outofstock'),
'operator' => 'NOT IN'
) ),
) );
2) 包括元查询 like:
$products = new WP_Query( array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'posts_per_page' => 6,
'orderby' =>'meta_value_num',
'order' => 'DESC',
'meta_query' => array( array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '!=',
) ),
) );
两种方法都有效。