How to: Exclude posts or pages from search results

December 21, 2008 at 8:28 am

Would you like to be able to control which post or pages must be exclued from searches on your WordPress blog? If yes, this recipe will probably help you a lot. Just read on.

To achieve this recipe, simply paste the following code on the functions.php file from your theme. In this example, posts with IDs 8 and 15 will be excluded from your blog’s search results:

function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('cat','8,15');
    }
    return $query;
}

add_filter('pre_get_posts','SearchFilter');

Credits goes to Kyle Eslick for this awesome hack!