More
    HomeDesign / DevHow to Exclude Pages from Search Results

    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.

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

    [sourcecode language=’php’]

    - Advertisement -

    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.

    - Advertisement -
    WPTidBits
    WPTidBitshttps://wptidbits.com/
    Owner of WPTidBits. Totally in love with WordPress!

    3 COMMENTS

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here

    110FansLike
    761FollowersFollow