Post Pic

How to: Exclude posts or pages from search results

Would you like to be able to control which post or pages must be exclued from searches on your WordPress blog? If yes, this recipe will probably help you a lot. Just read on.

To achieve this recipe, simply paste the following code on the functions.php file from your theme. In this example, posts with IDs 8 and 15 will be excluded from your blog’s search results:

function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('cat','8,15');
    }
    return $query;
}

add_filter('pre_get_posts','SearchFilter');

Credits goes to Kyle Eslick for this awesome hack!

11 Responses

Sep 13 2011 12:44

Hi,

Thank you for this snippet! But so far it does not seem to work. I am using WP 3.2.1.

I want to remove 3 pages from my search results. However, the snippet does not seem to be working for pages. I have added in the IDs of my pages, but they still appear in my search results. I have also tried it for posts, but it does not remove posts from the search results.

Any ideas? (There is no date on this snippet, so I am not sure how old it is)

Nov 29 2011 14:22

@nicholas
the code in this post is partially wrong, if you want to exclude a cat (eg cat id 5 and 24) you have to set
$query->set(‘cat’,'-5,-24′);
if you want to exclude a post set
$query->set(‘p’,'-250′);

bye

Dec 31 2011 02:53

Is there a way to exclude a specific page by Page ID #?

Jan 13 2012 22:17

Yes. To exclude a page change

$query->set(‘cat’,’8,15′);

to

$query->set(‘post__not_in’, array(215));

where 215 is the ID of the page you want to exclude. You can exclude as many pages as you want, just add the IDs to the array. You can also use this to exclude posts.

Mar 01 2012 16:22

Hi. Is there a way to reverse this code? Exclude everything and and add the post_id I want to include? I have tons of post I want to exclude from search results so it would be efficient to make everything non-searchable and add only the post_id I want to include.

Big thanks.

Mar 12 2012 16:44

Hi, Thanks for this code but I can’t seem to omit specific pages from showing in the search results. The following is the code I am using. Any help would be appreciated.

add_filter(‘pre_get_posts’,'SearchFilter’);
function SearchFilter($query) {
if ($query->is_search) {
$query->set(‘post__not_in’, array(1449,1447,1445,1443,1441,1439,1437,1435,1433,1431,438));
}
return $query;
}

Am I doing something wrong?

May 09 2012 19:07

Updated.

// search filter
function fb_search_filter($query) {
if ( !$query->is_admin && $query->is_search) {
$query->set(‘post__not_in’, array(40, 9) ); // id of page or post
}
return $query;
}
add_filter( ‘pre_get_posts’, ‘fb_search_filter’ );

Jul 14 2012 00:34

Hi… I just tried the last bit of code, from Wallace… But it’s not working for me. I am pretty desperate to eliminate some pages and posts from search, as they are landing pages specifically made for Google and de-indexed from the googlebot.

I think WP should build in: if the page is no-index, don’t include it in search.

Aug 29 2012 15:07

Excellent! `post__not_in` filter worked for me:

function sth_search_filter($query) {
$excluded_posts=array(
sthcorp_options()->cta_post,
sthcorp_options()->col_left_post,
sthcorp_options()->col_mid_post,
sthcorp_options()->col_right_post
);
if($query->is_search)
$query->set(‘post__not_in’,$excluded_posts);
return $query;
}
add_filter(‘pre_get_posts’,'sth_search_filter’);

In my case there are 4 posts serving as `static blocks` which get loaded onto the home page. Those post IDs are registered with WP in the Dashboard under Appearance->Theme Options. I don’t want these to be searchable, so my first line in the function grabs those IDs and puts them into the $excluded_posts array.

Thanks for the cool tip!

Dec 18 2012 23:16

I’ve noticed that unless caching is turned off, the changes do not reflect immediately. So either turn caching off or open a new browser and attempt your changes.

Once I did that, the code with the top post and the modified comment worked perfectly.

Mar 01 2013 08:11

Excellent code. I used the $query->set(‘cat’,’-5,-24′); and it worked like a charm!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox