Exclude a Page From Search

Posted on Nov 13, 2011 in Plugins, WordPress | 0 comments

exclude web page from searchIn some themes, you may have special pages or posts that only appear in a particular location in the theme—perhaps a blurb on the home page, for example. You may be surprised to see this page appear when people search the posts of your site!

There are two main ways to hide specific posts from searches: you can modify the PHP code for your theme, or use a plugin.

Write Code to Remove Pages From Search Results

WPMods has an excellent tutorial on excluding posts and pages from search. You can see the full explanation on their site, but I’ll copy and paste the relevant code here for those who are comfortable customizing PHP code.

Why hard-code it instead of using a plugin? Plugins can use more “overhead”—if you have many plugins, you may slow down your site.

// search filter
function my_search_filter($query) {
// make sure we are not in the admin and that we are performing a search
if ( !$query->is_admin && $query->is_search) {
$query->set('post__not_in', array(40, 9) ); // IDs of pages or posts
}
return $query;
}
add_filter( 'pre_get_posts', 'my_search_filter' );

Note: to find the page ID, edit the relevant page or post in the admin console, then just take the number from the URL. If the URL is /wp-admin/post.php?post=12345&action=edit, then the ID you need to add into the code is 12345. Just separate the relevant IDs with commas.

Simply Exclude Pages from Search with a Plugin

The disadvantage of writing code to modify your site, apart from programming complexity and potentially introducing bugs, is that if you need to restore the site or copy it somewhere you may not think of the change. If you upgrade the theme, you would lose all your modifications True, you can get into the habit of creating child themes, but that is far from a perfect solution.

And hey, it’s just easier to drop in a plugin and call it a day.

My current recommended plugin for this purpose is the Simply Exclude plugin. That is because, true to its name, it is simple. There is a settings screen with advanced options, but all you really need to do is install the plugin, go to the post you want to exclude, and change the setting in the “Simply Exclude” box to hide it. As simple as that.

However, depending on your theme or other plugins, it’s possible you may have some sort of conflict or other issue. As always, try out the plugin on your test site first—and if this one doesn’t work for you, search the WordPress Plugins Directory. There are plenty of other options, this one is just very simply.

Do you have another solution? Have you experienced problems with these? Let me know in the comments.

Submit a Comment

Manage your comment subscriptions