我有这个代码,它显示了最好的销售,但它也显示了没有库存的产品,我该如何修改代码。这样它只显示有库存的产品? .谢谢!
$best_sellers_args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'posts_per_page' => 6,
'orderby' =>'meta_value_num',
'order' => 'DESC'
);
$products = new WP_Query( $best_sellers_args );
最佳答案
从 WooCommerce 3 开始,有两种方法可以在您的 WP_Query
中排除“缺货”产品:
1) 包括一个税务查询,例如:
$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) 包含一个元查询,例如:
$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' => '!=',
) ),
) );
两种方式都有效。
关于php - 使用 WP_Query 仅显示库存产品中的 WooCommerce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56137935/