Exclude pages from wordpress search

If you want to exclude wordpress pages from the search then you have to add following code in your themes function.php file.

    function SearchFilter($query) {
    if ($query->is_search) {
    $query->set('post_type', 'post');
    }
    return $query;
    }

    add_filter('pre_get_posts','SearchFilter');

Above code filters all the posts retrieved by the wordpress query and set post_type equal to “post”.

Source

WordPress Filters
bavotasan

Leave a Reply

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