For various reasons, we may need to exclude certain pages from WordPress search results. This isn’t the same as excluding all pages from the search results. We only want to exclude specific pages. Here’s one way to do that. Add this code to your child theme’s functions.php, or better yet make it a plugin. If you don’t know how to make a custom plugin, you can do that easily with Pluginception.
Don’t want to manage this manually, or not comfortable adding custom code to your site? Check out Search Control for WordPress. It makes this much easier for you!
function jp_search_filter( $query ) { if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) { $query->set( 'post__not_in', array( 10,11,20,105 ) ); } } add_action( 'pre_get_posts', 'jp_search_filter' );
In the example above, we’re excluding pages with IDs 10, 11, 20, and 105 from the search results. You will need to change these numbers to match the IDs of the pages you want to exclude.
The WP_Query class in WordPress is a powerful tool we have at our disposal for customizing the content shown on our sites. Check out the WP_Query documentation to see all the things it can do.
It would help people if you mention they should put this clause in functions.php
You’re right. I assumed people looking for this would know, but I should mention that. Thanks.
Thank you so much, this worked a treat.
Hi,
Thanks a lot for the code, actually my requirement was to exclude some post as well as categories so i had mentioned the following ways. Mentioned below is the code for your reference.
Thanks for sharing your use case, Shraddhananda.
Thank you very much the code worked for me. May you a thousand more smiles in your life time.
Thanks, Jeff! You too! :)
not working for me for some reason
The is_main_query function ensures the query you’re targeting is the “main” one on the page, and not some custom secondary query. It should work on your site with this in place unless you have some other code tinkering with queries.
Brilliant, thanks very much. Just what I needed.
It works great :) thanks so much …
I just wanted to know if there is some way to hide the author information for posts/pages in wordpress search results.
PS could I add it to the same plugin?
Thanks
Ryan, hiding that information would require some customizations to your theme, preferably in a child theme. The theme controls how the search results are displayed. This code just controls the search visibility of the specified pages.
The problem with this approach is that it will also hide posts/pages from the Admin panel search. This isn’t very helpful if you have a lot of posts/pages on your site and you need to search for a specific post/page which you have omitted from appearing in the search results.
To get round this, add the following to the conditional statement:
!$query->is_admin
So the end result would be:
function jp_search_filter( $query ) {
if ( !$query->is_admin && $query->is_search && $query->is_main_query() ) {
$query->set( ‘post__not_in’, array( 10,11,20,105 ) );
}
}
add_action( ‘pre_get_posts’, ‘jp_search_filter’ );
Hey dt.k, you’re absolutely correct. I didn’t make that clear. I’ve updated the snippet above. Thanks for the feedback!
Than you much John for this snippet and GREAT catch dt.k!