Preventing certain posts from appearing in WordPress search results

Last updated on November 28, 2011. Tags: ,

I discussed in the previous article on how to prevent static pages from appearing in WordPress search results using the theme file functions.php. A much earlier article already discussed how to exclude certain pages OR posts from the search results, and I mentioned in the previous article that the earlier method has some problems.

In this article, I will discuss how to apply a similar technique using the functions.php, this time, to block certain  WordPress posts from appearing in search results.

So how would I stop certain posts from being listed in search results?

This technique is much easier because we  need to edit the source code of the theme files only one time. An important component here is a special category that is only for hidden posts. Whenever you have a new post that you want to prevent from appearing in search results, all you need to do is put that post under that special category.

First, create the special category just like how you would create any normal category. I suggest that you name it in such a way that it is either at the bottom or at the top of the list of categories in the Dashboard so that you don't mix it up with other categories, such as AAAHidden or ZZHidden (the category list is in alphabetical order). Take note of the ID of this category.

Next, open the functions.php and include the following code between the <?php and ?> tags, in the manner similar to the previous article. The 10 in the code below is the category ID of the special category. Just replace it with whatever ID WordPress assigned to your special category.

function SearchFilter($query) {
if ($query->is_search) {
$query->set('cat','-10');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');

In the code above, -10 means all categories except for the one with category ID = 10 will be included in the search results. The minus sign is important. Without it, what will happen is that search will show results ONLY under category ID=10.

Assign all the posts that you do not want to appear in the search results under the special category. Note that when you assign a post under this category, even if it is also assigned to another category, it will still not appear in the search results.

Once you assign posts in special category, it will appear in the category list of your website (not the one in the dashboard mentioned earlier). Since this category isn't meant to stand as one of the categories you use in organizing your posts, you might need to prevent it from appearing in the category list.

This method was tested on WordPress ver. 2.8.5 and ver. 2.9.1. It is highly likely that it would work in closely related versions and in all versions between these two.

Why would I not want certain posts from appearing in search results?

Good question! I use this technique only on posts that are not intended to appear as an individual post such as the sticky note intended as welcome message, like the header saying "Welcome to Codegrad" and the paragraph immediately below it in the home page. It is actually an individual post, and I can edit it by just opening the said post. However, when you use the search function on the right, there's no chance for the sticky message to appear.

Addendum: July 27, 2010

In case you want to prevent posts belonging two or more categories from appearing in the search results. You indicate their category IDs in the third line, separated by comma, as shown below:

function SearchFilter($query) {
if ($query->is_search) {
$query->set('cat','-10,-20,-30');

Posted by Greten on August 21, 2009 under WordPress

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Read Comments

  1. Posted by George Plumley on 10.19.10 6:24 pm

    Got the function working just fine – thanks for posting this tip. One thing to note is that the search on the admin side of WP was also getting filtered.
    So I added an IF statement within the function to check if the visitor is logged in. If not, then the search gets filtered. If you are logged in, then the search does not get filtered.

  2. Posted by Greten on 10.27.10 4:18 pm

    Hi George, I personally do not have any need for such IF statement (checking if the user is logged in, that is) because I can always use the aforementioned “special category” to view the hidden posts, and I usually don’t have that many hidden posts, but it might be interesting for the other visitors of this blog if you can share with us how you did it. I checked your site but I could not find anything about it. Thanks!

Post Comments





Comment Rules and Reminders

  • The links to the commentator's e-mail do not have nofollow tag. However, I will be very strict in approving comments.
  • When you comment, please say something that indicates that you indeed read my post. If your comment is a general statement that can fit to any blog post about any topic, it will be regarded as spam.
  • What you write in the name field may include keywords to your website provided that (1) it's only up to four words long and (2) at least one of these four words is your first name or nickname. I rather reply to Bob or to Joe Smith than to Online Marketing Tips.
  • Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.
  • Fields marked with asterisks (*) are required. Your email will never be displayed in public.