Exclude pages or include specific categories in WordPress search

Add this code to your functions.php file to exclude pages from your search results:

/* Exclude pages from Search */
function vibeExcludePages($query) {
	if ($query->is_search) {
        $query->set('post_type', 'post');
    }
    return $query;
}
add_filter('pre_get_posts','vibeExcludePages');

Or add this code to include only certain categories in your search results:

/* Include certain categories in Search */
function SearchFilter($query) {
	if ($query->is_search) {
		$query->set('cat','3');
	}
	return $query;
}
add_filter('pre_get_posts','SearchFilter');