您能帮我吗?我有一个产品列表,我想要隐藏在产品的可见性详细信息上标记为隐藏的那些产品。这是我的代码:
$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>";
}
我要显示所有标记为可见的产品。
答案 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>";
}
经过测试可以正常工作。