使用woocommerce从循环中排除产品
问题描述:
我必须使用woocommerce排除类别循环产品页面中的一些产品。只有那些产品,在元表中具有特定的价值才会被排除。我写了下面的代码,但这不适合我。请有人帮助我。使用woocommerce从循环中排除产品
add_filter('pre_get_posts', 'custom_pre_get_posts_query');
function custom_pre_get_posts_query($q) {
if (! $q->is_main_query()) return;
if (! $q->is_post_type_archive()) return;
$q->set('meta_query', array(array(
array(
'key' => '_auction_closed',
'compare' => 'NOT EXISTS'
)
)
)
);
remove_filter('pre_get_posts', 'custom_pre_get_posts_query');
}
答
尝试下面的代码:
add_filter('pre_get_posts', 'custom_pre_get_posts_query');
function custom_pre_get_posts_query($q) {
if (! $q->is_main_query()) return;
if (! $q->is_post_type_archive()) return;
$meta_query = $q->get('meta_query');
$meta_query[] = array(
'key'=>'_auction_closed',
'compare'=>'NOT EXISTS',
);
$q->set('meta_query',$meta_query);
remove_filter('pre_get_posts', 'custom_pre_get_posts_query');
}
遗憾不是为我工作......... – 2014-10-17 13:55:37
它在这里工作的罚款。在数据库中检查一次meta_key _auction_closed“是否存在于postmeta表中? – 2014-10-17 16:12:33
该功能的主要目的是显示没有元键'_auction_closed'的产品。我的一些产品有meta key' _auction_closed',但有些没有,我只需要展示那些没有这个meta key的产品,而这在我的使用woocommerce的分类页面上没有作用。 – 2014-10-18 05:56:58