Forums » Fixing WordPress » Excluding Pages from Search Results

  • Genevieve

    (@sweetgenevieve)


    I customized a WP installation for some clients so that they have a private section of the site that only their registered clients can view. The trouble is that private data shows up in search results for just anyone. Is there a way to exclude certain posts and/or pages from being searched? I searched the forums and the codex and found a plugin that will do this, but I was hoping for a non-plugin way to handle this (such as editing the search code or the search template). Any ideas?

Viewing 4 replies - 1 through 4 (of 4 total)
  • what plugin did you find?

    Thread Starter Genevieve

    (@sweetgenevieve)

    It’s called search unleashed. Looks like a great plugin, but it’s kind of overkill for the site I want to use it for. The only option I really need out of it is the ability to exclude certain content from being searched.

    Here is the best and easiest plugin I have found for excluding post, pages or categoeies from being searched!

    http://wordpress.org/extend/plugins/simply-exclude/

    There’s a great tutorial for excluding pages from search results here.

    In short, just add this to your theme’s functions.php file (or create the file if it doesn’t exist):

    <?php
    function mySearchFilter($query) {
    if ($query->is_search) {
    $query->set('post_type', 'post');
    }
    return $query;
    }
    
    add_filter('pre_get_posts','mySearchFilter');
    ?>

    It should be possible to adapt to exclude selected posts too but the author hasn’t gotten around to describing that yet.

    I’d be very interested to know how to combine the page exclude function above with the ability to exclude posts by tag. I’m guessing there’s a simple solution?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Excluding Pages from Search Results’ is closed to new replies.