Exclude Posts from Search Results


How to Exclude Posts from Search Results

I thought I’d stick a post together for this, but to be honest I’m tempted to really call it ‘Exclude Posts from Search Results – A Classic Example of Excessive Plugin Use’.

There can be good reasons why you’d want to exclude certain post categories from the search results in WordPress – the trouble is that if you Google it you end up looking through the plugin repository and you might end up installing one of them.  I was looking at one just now.  Don’t get me wrong, I have the greatest of respect for anyone who writes a plugin and uploads it for general use, keeps it updated, supports users in the forums.  Hats off to them, it can be a lot of work and effort for very little thanks and next to nothing in financial terms.

Why plugin is better option than writing code

It does engender a plugin dependency amongst users though, and I’ll include in that category the WordPress ‘developers’ who are really designers trying to get away with it.  Let’s face it, even real developers often just don’t have the time to code up something from scratch, or there’s no money in the pot to cover such a thing and they have to go looking for a suitable plugin to hack.

Drawbacks of Plugins

However, by doing this they often end up loading in tonnes of code which isn’t being used.  Code which might end up offering a security hole as the world evolves but the plugin doesn’t, just look at timthumb as an example of that.  And that assumes that it’s a well intentioned plugin in the first place, not one full of BASE64 encoded sneaky linkbacks or even worse.

Anyway, back to the title of the post – I looked at this plugin that had an FAQ for it’s FAQ (OK, I’m exaggerating, but it wasn’t far off), that generated a huge complicated admin page using it’s 3013 line long php file that loaded in jQuery UI plus AJAX files, the main JavaScript file, a CSS directory, and language files.  And it didn’t seem to work properly.

Lets Get Started

Here’s the code that was really required to get the job done, included in the functions.php file, the post categories that I wanted excluded.

function SearchFilter($query) {
if ($query->is_search) {
return $query;


Right, that’s the end of my rant about bloated WordPress plugins that try to do a lot more than they should.

EDIT. As an addition to this it’s easy to alter it so that it excludes specific pages rather than category IDs.

$query->set('post__not_in', array( 4,10,182 ));