How To Noindex WordPress Subpages

A technique commonly used in Search Engine Optimization (SEO) is to tell the search engines that you don't want particular pages to show up in the search results.

The code you need in the HTML of the page is:

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

Because the directive is to "noindex, follow", most people colloquially refer to that as "noindexing" the page.

The easiest way to noindex particular pages is to use a plugin like Yoast SEO.

Yoast SEO used to give you an option to noindex subpages, but they removed it in February 2018.

The team at Yoast have their reasons for removing this option, but I want it, so this is how I coded it.

How to set WordPress subpages to noindex

Add this code to the functions.php file of your child theme:

add_action('wp_head', 'robot_meta_subpage_fix', 1);
function robot_meta_subpage_fix() {
  if(is_paged()) {
    echo "<meta name='robots' content='noindex, follow'/>", "\n";
  }
}

That will add a noindex tag just to subpages, like this:

It won't add the noindex to any other pages on your website. Just the subpages.

How to replace Yoast SEO's robots tag on subpages

The problem with Yoast's implementation is that the default subpage has this robots directive:

In case that's too small, it says:

<meta name="robots" content="max-snippet:-1, max-image-preview:large, max-video-preview:-1"/>

That robot directive isn't removed when you add your own.

We want to remove the default Yoast robots meta code for subpages.

Otherwise we end up with something that looks like this:

If you want to remove the Yoast SEO robots tag for subpages and add your own, paste this code into the functions.php file of your Child Theme:

add_action('wp_head', 'robot_meta_subpage_fix', 1);
function robot_meta_subpage_fix() {
  if(is_paged()) {
    $wpseo = WPSEO_Frontend::get_instance();
    remove_action( 'wpseo_head', array( $wpseo, 'robots' ), 10 );
    echo "<meta name='robots' content='noindex, follow'/>", "\n";
  }
}

You'll end up with a header that looks somewhat like this:

Mike Haydon

Thanks for checking out my WordPress and coding tutorials. If you've found these tutorials useful, why not consider supporting my work?

Buy me a coffee

2 thoughts on “How To Noindex WordPress Subpages”

  1. Awesome!

    Thank you Mike for this solution. Not sure why would Yoast remove this option. Some sites have pretty huge sub pages of categories and tags and it makes no sense to have them in Google Index.

    Reply

Leave a Comment