从wordpress全球搜索结果中排除一个wooccommerce产品类别



我使用此代码从wordpress搜索结果中排除一些帖子类别:

function SearchFilter($query)
{
if ($query->is_search)
{
$query->set('cat', '-709,-710,-614');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');

我的问题是,它不适用于wooccommerce类别,产品也没有经过过滤。我如何也过滤一些wooccommerce类别?

您可以替换您的类别吗。希望这对你有所帮助。

function wpse188669_pre_get_posts( $query ) {
if (
! is_admin()
&& $query->is_main_query()
&& $query->is_search()
) {
$query->set( 'post_type', array( 'product' ) );
// set your parameters according to
// https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
$tax_query = array(
array(
// likely what you are after
'taxonomy' => 'product_cat',
'field'   => 'slug',
'terms'   => 'category-2',
'operator' => 'NOT IN',
),
);
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'wpse188669_pre_get_posts' );

最新更新