Hi,
Try this code snippet in your functions file and add there the category id you want to exclude:
function wcs_exclude_category_search( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if ( $query->is_search ) {
$query->set( 'cat', '-22, -21' );
}
}
add_action( 'pre_get_posts', 'wcs_exclude_category_search', 1 );
This is tested so it should work fine for you also.
Thanks
-
This reply was modified 11 years by
Lucian.
Hello Lucian,
I am bringing up his tread one more.
I apologize for my misunderstanding, but where exactly does the category id number has to be placed within the code You provided?
Thank You!
Hello Catalin,
Thanks for the help!
But it doesn’t seem to be working. Maybe I have put the code in the wrong functions.php code line?:
http://www.screencast.com/users/mrbubble/folders/Default/media/4861c417-8a00-4d9d-82f4-477f9e3cc01e
Thanks!
In the end I added the category’s name.
function exclude_category_from_search($query) {
if ($query->is_search) {
$cat_id = get_cat_ID(‘uncategorized’);
$query->set(‘cat’, ‘-1’);
}
return $query;
}
add_filter(‘pre_get_posts’,’exclude_category_from_search’);
It worked!
Thanks!
