【发布时间】:2017-01-11 21:27:06
【问题描述】:
试图从我的 WooCommerce 商店页面中排除单个类别
我正在使用此代码,但它会破坏我网站的属性过滤器链接:
add_action( '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;
if ( ! is_admin() && is_shop() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'samples' ),
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
【问题讨论】:
-
我相信商店页面是主要查询,它绝对是一个帖子类型的存档,所以那些会阻止它在商店存档上工作。
-
@CarlosDaniel ... 这意味着您应该删除
if ( ! $q->is_main_query() ) return;和if ( ! $q->is_post_type_archive() ) return;... :)
标签: php wordpress woocommerce