我有一个carousel插件,可以执行各种操作,它只显示已发布的产品:
$common_args = array(
'post_type' => 'product',
'posts_per_page' => !empty($posts_per_page) ? intval($posts_per_page) : 4,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
);
但我需要它来排除“隐藏”的产品,这些产品在技术上仍然只是不可见。或者,如果它排除了特定类别的产品(我的所有隐藏产品都属于两个特定类别),我可以使用它。
我该怎么办?
自Woocommerce 3以来,产品可见性由product_visibility
术语exclude-from-catalog
分类处理,因此您需要添加以下税务查询:
$common_args = array(
'post_type' => 'product',
'posts_per_page' => !empty($posts_per_page) ? intval($posts_per_page) : 4,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'tax_query' => array( array(
'taxonomy' => 'product_visibility',
'terms' => array('exclude-from-catalog'),
'field' => 'name',
'operator' => 'NOT IN',
) ),
);
它应该工作。用WordPress get_post()
函数测试了这个参数数组(它的工作原理)。