How to Noindex Archive Pages in WordPress

How to Noindex Archive Pages in WordPress

Have you ever tried to search for something on your blog on Google and been presented with multiple search results sharing the same content? Say for example you search for the title of a post and you get not just the page itself, but also two or three archive pages, a category page, and maybe even the search page! Needless to say, these can be very confusing for someone visiting your site from Google especially considering that archives change all the time and they may not even get the content that they’re looking for when they click on the link. In addition, search engines like Google are wasting their time crawling your website hundreds or thousands of times a day spidering content that is pretty much useless.

Take a look at the screenshot below for an example of what I’m talking about.

unwanted resulst

Do visitors really need to know that the search page exists? What value is added by seeing successive pages of content with no additional information? In my opinion, none.

So how do you prevent this from happening? How do you ensure that search engines only index main content pages and not unrelated categories or archives? This is possible using what is known as the “noindex” meta-attribute. All it requires is a single entry in your header.php file located in your themes directory. Here are two ways to insert the necessary code.

Noindexing your Archive Pages

Every theme includes what is known as a header.php file. It outputs critical information such as initialization scripts, in-line CSS etc. Various themes and plug-ins “hook” into header.php to insert their own content. In order to tell search engines that we don’t want them to index a certain page, we merely have to put in the following line:

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

The trick is to make sure that this is ONLY placed on archive pages and not on your regular content. Otherwise nothing would be indexed and your blog would just vanish from the search results! To do this, you’re going to make use of the function known as “is_archive”. As per the WordPress documentation, it returns true if the current page has “archive” type content. It covers all category, tags, and date based links.

In your WordPress dashboard, go to Appearance, and click “Edit”. In the list of theme pages on the right-hand side, scroll down to search for and click header.php as shown in the screenshot below.

locate header php

Once you have the file available for editing, search for the closing, </head> tag and insert the following code:

<?php
if(is_archive()) { ?>
 <meta name=’robots’ content=’noindex,follow’ />
<?php }?>

Insert code

As you can see, this first checks whether or not the current page is an archive, and then selectively outputs the required line that tells search engines not to index it. Make sure you take a backup of your existing header.php file first to restore in case something goes wrong.

In the screenshot below, our code is a success. The appropriate line has been added to a category-based archive and it will no longer appear in Google’s search results.

Success

Using Functions.php

Another alternative is to hook into the wp_head function yourself and add the code you want. This has the advantage of not having to directly mess with the WordPress themes especially if you have abstracted functions.php into a separate plug-in (which is strongly recommended).

If you choose to go down this path, we simply have to add the following into our custom functions.php file:

add_action( 'wp_head', 'add_noindex' );
function add_noindex() {
if(is_archive()) {
echo '<meta name="robots" content="noindex,follow" />';
}
}

Easy! Now your changes will not be overwritten if you modify your theme or upgrade to a new version. Overall, the second solution is more elegant than the first. But if you don’t want to get into creating your own plug-in or don’t have FTP access, the first tweak directly editing header.php will work fine as well. You just have to make sure that you redo your modifications in case you ever upgrade your theme.

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.