我在这里搜索了“courses”,并且由于下面的代码段处于活动状态,因此“课程”类别中不会显示任何搜索结果(WooCommerce 产品)。
add_action( 'pre_get_posts', 'exclude_category_woocommerce_search' );
function exclude_category_woocommerce_search( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_search() ) return;
if ( ! is_admin() ) {
$q->set( 'tax_query', array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'courses' ), // 更改 'courses' 为你的类别别名
'operator' => 'NOT IN'
)));
}
}