Categories
Design / Dev

How to Exclude Pages from Search Results

By default, all the search results from Wordpress will include pages and posts. However, we might want to be able to exclude pages from the search results, and bring results only from posts. To change this default behavior, it is actually quite simple.

By default, all the search results from WordPress will include pages and posts. However, we might want to be able to exclude pages from the search results, and bring results only from posts. To change this default behavior, it is actually quite simple.

Include the following lines of code in your functions.php  file.

[sourcecode language=’php’]

function vibeExcludePages($query) {
if ($query->is_search) {
$query->set(‘post_type’, ‘post’);
}
return $query;
}
add_filter(‘pre_get_posts’,’vibeExcludePages’);

[/sourcecode]

By adding this bit of code, it will basically tell your search results to not display anything but posts.

Avatar

By WPTidBits

Owner of WPTidBits. Totally in love with WordPress!

3 replies on “How to Exclude Pages from Search Results”

Leave a Reply

Your email address will not be published. Required fields are marked *