WordPress Tips: Excluding Pages/Posts From Searches

It’s very, very easy to remove certain posts, and/or pages from your search results. Although I guess it’s only really easy if you know how, so instead of keeping it all hush, hush here is the secret.

Where Does It Go?

These code snippets would be placed in search.php, if that file is not there then searches will use your index.php file. Should the latter be true you will need to limit the code to running only when a search is being made. To do that use the following bit of code.

Keeping WordPress’ Default Query

The big problem most people have is that they must keep WordPress’ default query intact or the search results will be destroyed when you create your new query. This is actually documented on the codex, but I’ll try to simplify it a bit more:

This takes your new options in array format & merges them with the original WordPress query, that keeps your search results intact but adds in your options.

Excluding Pages & Posts

We can now use this to exclude any pages & posts we want. First write yourself a list of all of the ID’s of the posts/pages you want to exclude. Then add them to your code like so:

Although it does say post__not_in it does exclude both posts & pages.

Excluding All Pages

Of course there is also the possibility that you do not want to show any pages at all, a good example is if you run a blog & your pages are just for things like about us, privacy policy & disclaimers. You can do that simply by telling the query to only retrieve posts.

Well that’s it. I hope this tip has helped you or inspired you in some way. If you have any questions drop me a comment or send me a email & I’ll help you asap.

written by

WordPress wizard, coding ninja and all round cool guy. Loves Sci-Fi, geeky stuff, and of course Firefly. Currently a self confessed addict of Korean & Japanese Pop.

Advertisement

10 Comments

Author’s gravatar

Thanks for clarifying how this gets done. But — and sorry to be the guy who just asks without contributing something — have you had any luck with excluding whole *post categories* from WP search? I’ve had pretty mixed results, and admittedly, the one time I got it to work was by accident. Biggest problem seems to be pagination, because I want to limit those unwanted categories out by default…not pluck-remove them from the big (already-been-paginated) catch. Any ideas?

Reply
Author’s gravatar

Well I haven’t had a chance to try it, but something like this would probably work:

Again I haven’t had the chance to try it, but I can’t see any reason why it should work to exclude the categories you specify.

This will update the query & re-save it into the WP object cache, so you won’t be removing them after the fact. Also because you are merging the original query you shouldn’t have any paginate problems.

Hope that helps, let me know how it goes. ;)

Reply
Author’s gravatar

I want to exclude the Posts which i have included in slider. Those should not appear in search, default post listing in the loop etc etc except single page. Any idea how can i achieve it?

Reply
Author’s gravatar

That’s a little complicated, but the method depends. You can either use a normal loop then use conditionals like this:

Or you can use query_posts() before the loop to exclude the category. Check out the WP Codex page for query_posts() to find out how to exclude categories.

Either way you’ll have to add it to all the loops you want to exclude it from. There may be a way to do it using filters but I haven’t come across it yet.

Author’s gravatar

Hi,

I’m a noobi… so, sorry for my next question? =)

Can i have more than one search form?

Im using wordpres as CMS, so i need one search form just to find in the blog section and another search form just to find for the custom post type “products”…

Thx…

Reply
Author’s gravatar

Hi Luichy,

To be honest that isn’t a noob question, lol. It’s actually quite difficult to do. It’s also slightly long for a comment so I’ve created a post on how to have multiple searches in WordPress. :)

Author’s gravatar

Hi Paul,

Thx for the fast reply… =)
i already found the same solution you show, using the hidden label for an extra value.

The pretty amazing part is all the things i did implementing this code.

Before, i didn’t knew how to create redirections to custom template for archives or others situations, but now is sow easy… just a few conditionals and that’s it!

I’m building my first site with CMS for a customer, using the latest version of WP and trying to follow the lastest features it provides as Custom Post Type and Custom Taxonomies ( for Products Showcase).

http://www.onmak.bloque.com

I would like to get some critics of the job im doing (but remember… my first wp theme at all)

Friday i’ll help a bit to keep your server running ;)

Author’s gravatar

No worries.

I didn’t realize someone else had already done it. I don’t like to repeat content someone else has done unless I can show an alternative method. Glad you found a solution though.

WordPress is a great framework for building most types of website now thanks to their new CMS style features. :)

I’d love to give you some feedback on your site, but I get dropped onto a Warner Bros landing page when I try to visit your URL.

Thank you for any donation. They all help towards the server costs. :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">