How To Exclude Pages From WordPress Search

WordPress search results displays all the posts and pages that match the search query. However, the way WordPress is used, pages don’t have much useful information.

Pages are used for permanent information about the site like About, Contact page etc. So, its a good idea to exclude pages from WordPress search results.

To exclude Pages from WordPress search, paste this code snippet in your theme’s functions.php file,

1
2
3
4
5
6
7
8
function SearchFilter($query) {
    if ($query->is_search) {
    $query->set('post_type', 'post');
    }
    return $query;
    }

add_filter('pre_get_posts','SearchFilter');

That’s it. Now WordPress will exclude pages for all search queries.