All the credit for this bit of code goes to, where it was found.

To exclude one or more categories from WordPress search results, paste this into functions.php of your current theme:

function SearchFilter($query) {
if ($query->is_search) {
return $query;

Guys from had one small mistake in their code: $query->set('cat','1,10');. Category IDs need to have a “minus” sign to get excluded.