• Resolved haydenhancock

    (@haydenhancock)


    After doing some research I have found some similar problems but not actual solutions. I am trying to exclude a page from the search results without the use of a plugin.

    The following is code that I’ve found only shows the results from the page I want to exclude:

    // search filter
    function my_search_filter($wp_query) {
       if ($wp_query->is_search) {
          $wp_query->set('page_id','27');
       }
       return $wp_query;
    }
    add_filter('pre_get_posts','my_search_filter');

    Does the “set” method not work for the page_id? This function only pulls the results from a page with id of 27. I want to exclude this page from my search results.

    Any help would be much appreciated.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter haydenhancock

    (@haydenhancock)

    Has anyone been able to figure this out?

    Try this:

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

    Thread Starter haydenhancock

    (@haydenhancock)

    @blueoxp,

    Thank you for your reply, however, this doesn’t work either. I could add a category, but I don’t want to add an extra step for the client to have to do. All the blog post will be private only and I don’t want people to be able to view them just because they forgot to select the posts category.

    My example doesn’t assume categories or even take them into consideration. 27 is the post or page ID.

    post__in is expecting an array of post or page IDs. I think you’re confusing it with in_category.

    Thread Starter haydenhancock

    (@haydenhancock)

    No, I understood what your function did and it made perfect sense. My original findings similar to this topic were based on categories. and I originally tried to modify the if statement to work with page exclusion instead of category exclusion. For example:

    // search filter
    function my_search_filter($wp_query) {
       if ($wp_query->is_search) {
          $wp_query->set('cat_id','27');
       }
       return $wp_query;
    }
    add_filter('pre_get_posts','my_search_filter');

    This function actually works for excluding categories in the search.

    Thread Starter haydenhancock

    (@haydenhancock)

    I guess I didn’t really explain it very well. Your code works properly.

    Here is a better explanation of what I am trying to do (I hope). I have a members only page for my website. This page is actually set to display all my “blog” posts. It will not contain any static information. Basically, I don’t want any post to display in the search.

    What would be the best way of going about this? Thanks again for your comments!

    Ah, try this where 27 is your blog category ID

    // search filter
    function my_search_filter($wp_query) {
       if ($wp_query->is_search) {
          $wp_query->set( 'category__not_in', '27' );
       }
       return $wp_query;
    }
    add_filter('pre_get_posts','my_search_filter');

    Thread Starter haydenhancock

    (@haydenhancock)

    I ended up doing the following:

    // search filter
    function my_search_filter($query) {
       if ($query->is_search) {
          $query->set('post__in',array(3,5,7,17,9,11,13));
       }
       return $query;
    }
    add_filter('pre_get_posts','my_search_filter');

    I am not sure if this is the most efficient way of doing it, but I think this will suite my needs. The search is only pulling from the pages that I want searched.

    I really appreciate your help blueoxp. Your suggestions have been most helpful!

    So it’s been awhile since you posted here, but here’s a solution I found today. In my specific case, I wanted to ignore any text that the user entered into the page that’s set as the blog posts page.

    function mySearchFilter($query) {
        if ($query->is_search) {
            $excludeId = get_option('page_for_posts');
            $query->set('post__not_in', array($excludeId));
        }
        return $query;
    }
    add_filter('pre_get_posts','mySearchFilter');
    Todd

    (@toddbeyond5280com)

    I needed to exclude one page from the results that is a modal and should only be viewed that way. I modified the code from Tim and thought I would put it here in case anyone else just has 1 page that needed to be excluded. 460 is the page id that needed to be excluded, so just replace that with the id of the page you want to exclude

    function mySearchFilter($query) {
        if ($query->is_search) {
            $excludeId = 460;
            $query->set('post__not_in', array($excludeId));
        }
        return $query;
    }
    add_filter('pre_get_posts','mySearchFilter');
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Excluding Page From Search’ is closed to new replies.