WordPress.org

Forums

[resolved] Possible to Exclude Pages from WP Search? (4 posts)

  1. Daniel Delos
    Member
    Posted 1 year ago #

    I want to exclude specific pages OR all pages from wordpress on-site search. Basically I want them only accessible through having their exact URL. Alternatively, I want WP search to ONLY show pages.

    Seems like it should be very simple. But over the last few hours I have tried every plugin claiming to do any of these things (they don't work with the latest wordpress version without exception). I have tested numerous methods that worked even up to a month ago. They don't now.

  2. Tara
    Volunteer Moderator
    Posted 1 year ago #

    try adding this to your theme's functions.php:

    function my_search_filter($wp_query) {
       if ($wp_query->is_search) {
          $wp_query->set('post__not_in', array( -101 ) );
       }
       return $wp_query;
    }
    add_filter('pre_get_posts','my_search_filter');

    where -101 is the post or page ID. change it your page id.

  3. Daniel Delos
    Member
    Posted 1 year ago #

    Thanks for the suggestion, I hope it will help anyone else searching. I came up with a different solution. I'm just using a subdomain for the form pages that I needed removed from the search.

  4. Tara
    Volunteer Moderator
    Posted 1 year ago #

    Glad you got it sorted :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.