How to Exclude Pages from Search Results in WordPress

Google+ Pinterest LinkedIn Tumblr +

Add the following code to your theme’s functions.php file.

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

 

You can also make it do the opposite by setting the post_type to pages, so it only return pages in the search results.

//Exclude posts from Search Results
if (!is_admin()) {
function wptj_search_filter($query) {
if ($query->is_search) {
$query->set('post_type', 'pages');
}
return $query;
}
add_filter('pre_get_posts','wptj_search_filter');
}

I hope this guide helped you to learn how to exclude pages or posts from WordPress search results. You can use this code to exclude any type of post, just change the post type.

Share.

Leave A Reply