而不是使用
pre_get_posts
钩子函数,您应该使用一个相关的专用Woocommerce操作和过滤器钩子。
请尝试以下操作:
add_filter('woocommerce_product_query_tax_query', 'custom_product_query_tax_query', 10, 2 );
function custom_product_query_tax_query( $tax_query, $query ) {
if( is_admin() ) return $tax_query;
// HERE Define your product category SLUGs to be excluded
$terms = array( 'ukategorisert' ); // SLUGs only
// The taxonomy for Product Categories
$taxonomy = 'product_cat';
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'slug', // Or 'name' or 'term_id'
'terms' => $terms,
'operator' => 'NOT IN', // Excluded
);
return $tax_query;
}
此代码继续运行。活动子主题(或主题)的php文件。它应该会起作用。