Created
October 3, 2020 04:55
-
-
Save jennlee20/4a029025bc5de5f4a821aa72e3527d15 to your computer and use it in GitHub Desktop.
[Wordpress] Exclude product category by slug in seach result.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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