I have content I want on my website, for example, insurance and real estate news, albeit I don’t necessarily want it displayed in search results for people who are not interested in news, albeit want advice on insurance and/or real estate written by someone related to 1 Reason Insurance directly.
The following code was added to the child theme and removes the categories from the search results. It took all of maybe 10 minutes to implement.
// remove categories from search added by Robert W 8/28/2021
function wcs_exclude_category_search( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if ( $query->is_search ) {
$query->set( ‘cat’, ‘-302, -65’ );
}
}
add_action( ‘pre_get_posts’, ‘wcs_exclude_category_search’, 1 );
In the line of $query->set (cat…..-302, -65 is the category numbers (can lookup using Post-> category to find) and separate categories by comma.