Using meta robots in a WordPress blog or website

Last updated on February 13, 2010. Tags: , ,

The meta robots is one of the well known techniques of preventing search engines from including some of your web pages in search results. To use meta robots to prevent search engines from indexing your web page, you simply insert the following codes anywhere between the header tags <head> and </head>.

<meta name="robots" content="noindex"/>

It's very easy to use this meta robots on static HTML web pages. All you need to do is to insert the code in web pages that you do not want the search engines to index, but what if you are using WordPress CMS?

The tweak that we are about to discuss here works in WordPress versions 2.6.1. and 2.9.1. It may or may not work in nearby versions and will most likely work in versions between them.

If you insert this code on header.php of your WordPress theme, search engines will not index your entire website (or blog) because the code will be placed in every page generated by WordPress: pages, posts, categories, search queries, etc.

If you don't want your entire WordPress-powered website to be included in search engines, you can just insert the meta robot above, but what if you want only certain pages in your website to be not included in search engines' search results such as the "thank-you" page and contact form?

Telling WordPress to insert meta robot only to specific posts or page

The solution is to apply PHP code to tell WordPress which pages (or posts) it will insert the meta robots. I presented several codes for several different possible scenarios below. You can insert any of these codes in header.php of your WordPress theme (must be between the header tags) depending on your purpose.

To prevent specific pages from being indexed:

<?php if ( is_page(55) ) { ?>
<meta name="robots" content="noindex"/>

<?php } ?>

In the code above, the post that we do not want search engines to index has a post ID of 55. You can simply replace 55 with the post ID of whatever post that you do not want to be seen in search engines' search results.

The same code can be used to prevent search engines from indexing certain posts. All you need to do is to replace is_page() with is_single():

<?php if ( is_single(10) ) { ?>

<meta name="robots" content="noindex"/>
<?php } ?>

Here, you can replace 10 with the actual post ID of the post that you do not wished to be indexed.

However, despite having unique post IDs for all pages and posts, WordPress still distinguishes between a page and a post. Hence, if you have a post that you do not want search engines to index, and WordPress assigned to it ID=10, you MUST use is_single(10). If you use is_page(10) while ID=10 is in fact, assigned to a post, WordPress will not insert the meta robot code.

Preventing more than one pages and posts from being indexed.

<?php if ( is_single(10) || is_page(39) || is_page(60) ) { ?>
<meta name="robots" content="noindex"/>
<?php } ?>

In the code above, search engines will not index the post with post ID=10 and the pages with post ID=39 and post ID=60. The sign || is the OR operator in PHP. You can have more than two or three pages/posts that the search engines would not index by applying the OR operator as many times as you need.

The PHP codes that I discussed in this section work by selecting which WordPress generated page it will insert the <meta name="robots" content="noindex"/>.

If you implemented this code in your WordPress-powered website, you would not see any trace that a PHP code is controlling the insertion of the meta robots. When you viewed the source code, you will only see the meta robots on specific pages and posts determined by the PHP code and not to all other pages and posts. Try it now and see for yourself.

Telling WordPress to insert meta robot only to specific type of dynamic pages

Prevent search engines from indexing all pages:

<?php if ( is_page() ) { ?>
<meta name="robots" content="noindex"/>

<?php } ?>

Prevent search engines from indexing all posts:

<?php if ( is_single() ) { ?>
<meta name="robots" content="noindex"/>
<?php } ?>

Prevent search engines from indexing all categories:

<?php if ( is_category() ) { ?>
<meta name="robots" content="noindex"/>
<?php } ?>

Prevent search engines from indexing search results from WordPress built-in search function:

<?php if ( is_search() ) { ?>
<meta name="robots" content="noindex"/>
<?php } ?>

You can also use the OR operator to implement meta robots to two or more types of dynamic pages.

<?php if ( is_single() || is_category() ) { ?>

<meta name="robots" content="noindex"/>
<?php } ?>

Other possible use of meta robots

Even though this article covers primarily how to use meta robots to prevent certain WordPress generated dynamic pages from being indexed, you can tweak these codes a little and use it for another purpose such as preventing search engine crawlers from following the links in your page or from creating a cache copy of your website. All you need to do is to replace the noindex with nofollow or noarchive respectively.

Posted by Greten on January 17, 2009 under Hypertext Mark-up Language (HTML), Search Engine Optimization (SEO), WordPress tweaks

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

Related Posts

You might also be interested (randomly generated):

Post Comments

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.





* Required. Your email will never be displayed in public.