Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jennlee20/4a029025bc5de5f4a821aa72e3527d15 to your computer and use it in GitHub Desktop.
Save jennlee20/4a029025bc5de5f4a821aa72e3527d15 to your computer and use it in GitHub Desktop.
[Wordpress] Exclude product category by slug in seach result.
<?php
function jenn_search_filter_pre_get_posts( $query ) {
if ($query->is_search()) {
$query->set( 'post_type', array( 'product' ) );
$tax_query = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'your-product-category-slug-1',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'your-product-category-slug-2',
'operator' => 'NOT IN',
),
);
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'jenn_search_filter_pre_get_posts', 1 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment