我正在开展一个项目,我需要在页面中列出所有隐藏的产品。我已经创建了一个短代码,我使用了以下元查询,但它不起作用。
$meta_query = array(
'key' => '_visibility',
'value' => array('hidden'),
'compare' => 'IN'
);
知道为什么这不起作用?提前感谢您的帮助。
答案 0 :(得分:1)
$args = array(
'post_type' => 'product',
'meta_key' => '_visibility',
'meta_value' => 'hidden'
);
$query = new WP_Query($args);
然后你通常的循环对我有效:)
答案 1 :(得分:0)
WooCommerce 3+的可见性为changed to a taxonomy instead of post meta,如果您使用WC()->query->get_tax_query()
函数调用来构建args,则只需从设置tax_query
中删除该调用即可。
如果您无权访问args或它们的构建方式并且已通过get_tax_query
运行,则可以使用过滤器删除NOT IN
征税查询以提高可见性:
add_filter( 'woocommerce_product_query_tax_query', 'smyles_show_all_products' );
// do something that calls some fn that makes product query
remove_filter( 'woocommerce_product_query_tax_query', 'smyles_show_all_products' );
function smyles_show_all_products( $tax_query ) {
foreach( (array) $tax_query as $t_index => $t_query ){
if( isset( $t_query['taxonomy'], $t_query['operator'] ) && $t_query['taxonomy'] === 'product_visibility' && $t_query['operator'] === 'NOT IN' ){
unset( $tax_query[ $t_index ] );
break;
}
}
return $tax_query;
}
答案 2 :(得分:-1)
使用此
$f1 = -1; // Last value read
$f2 = -1; // second to last value
$f3 = -1;
$a = array(1, 2, 3, 17, 27, 44, 45, 47, 49);
foreach ($a as $v) {
$f3 = $f2;
$f2 = $f1;
$f1 = $v;
if ($v == 45){
# tricky part
print "The last three v's were: " . $f1 . ", " . $f2 . ", " . $f3 . "\n";
}
}