Categories
Code WordPress

WordPress: Exclude Pages from Search Results

Limit the search results of your WordPress blog only to Posts.

Add the following to your functions.php file to exclude pages from the search results of your WordPress blog.

//Filter out pages from search results
function search_exclude_pages($query) {
 if ($query->is_search) {
 $query->set('post_type', 'post');
 }
 return $query;
}
add_filter('pre_get_posts','search_exclude_pages');

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.