Limit Search Results

Plugins, and Custom Post Types can add to your search results, and this isn’t always easy to adjust. You can edit your Post Type settings to exclude from search, but sometimes you need these items searchable in some areas of your website (like a directory), but not in the general search.

Control what search results are queried with the following function:

// filter search results
function filter_search_results($query) {
if (!$query->is_admin && $query->is_search) {
$query->set('post_type', array('product', 'post', 'page', 'author'));
}
return $query;
}
add_filter('pre_get_posts', 'filter_search_results');

Source.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.