Hi I am trying to exclude custom post category (specific category id) form the search query. My custom post category taxonomy name is review-cat
. I want exclude this custom post taxonomy specific id's.
function wcs_exclude_category_search( $query ) {
if ( $query->is_search ) {
$query->set( 'cat', '-33 -46' );
$query->set( 'post_type', array( 'review' ) );
}
return $query;
}
add_action( 'pre_get_posts', 'wcs_exclude_category_search', 1 );
I found this. It may help you https://stackoverflow.com/a/52140116/1053190
Or below code copy/paste from https://gist.github.com/billerickson/1332274
/**
* Exclude term from search query
* @author Bill Erickson
* @link http://www.billerickson.net/customize-the-wordpress-query/
*
* @param object $query
*/
function be_modify_search_query( $query ) {
global $wp_the_query;
if( $query === $wp_the_query && $query->is_search() ) {
$tax_query = array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'hidden',
'operator' => 'NOT IN',
)
);
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'be_modify_search_query' );