Categories
Pro Tips

Adding Noindex and Nofollow to Custom Post Types with Yoast SEO

Codeable.io

When managing a WordPress website, understanding how to control which pages are indexed by search engines is crucial. Sometimes, you might need to prevent certain types of content, like custom post types (CPTs), from being indexed or followed by search engines. This is particularly important for CPTs used for internal purposes or those not intended for public consumption. In this blog post, I’ll walk you through how to add noindex and nofollow attributes to a CPT using the Yoast SEO plugin and programmatically via your theme’s functions.php file.

Manually Adding Noindex and Nofollow with Yoast SEO

Yoast SEO, a popular WordPress SEO plugin, provides an easy-to-use interface to add noindex and nofollow tags to your CPTs. Here’s how:

  1. Navigate to Your CPT: Go to the WordPress dashboard and open the CPT you want to edit.
  2. Yoast SEO Meta Box: Scroll down to find the Yoast SEO meta box below the content editor.
  3. Access Advanced Settings: Click on the ‘Advanced’ tab within the Yoast SEO meta box.
  4. Set Meta Robots Index to Noindex: From the ‘Meta Robots Index’ dropdown, select ‘noindex’. This tells search engines not to index this particular CPT.
  5. Set Meta Robots Follow to Nofollow: Similarly, select ‘nofollow’ from the ‘Meta Robots Follow’ dropdown. This instructs search engines not to follow links on this CPT.
  6. Save Your Changes: Click the ‘Update’ button to apply these settings.

Programmatically Adding Noindex and Nofollow

If you have multiple posts within a CPT or prefer a more hands-off approach, you can automate this process with a snippet of code in your theme’s functions.php file:

/**
 * Add noindex, nofollow for CPT Premium (premium).
 */
function yoast_premium_no_index( $string = '' ) {
    $post_type = get_post_type();
    if ( $post_type ) {
        $post_type_data = get_post_type_object( $post_type );
        if ( 'premium' === $post_type_data->rewrite['slug'] ) {
            $string = 'noindex, nofollow';
        }
    }
    return $string;
}

add_filter( 'wpseo_robots', 'yoast_premium_no_index', 999 );

In this code:

  • We’re hooking into the wpseo_robots filter provided by Yoast SEO.
  • The function checks the current post type and applies noindex, nofollow if it matches our specified CPT (premium in this case).
  • This way, all posts under the ‘premium’ CPT automatically receive the noindex, nofollow settings without needing manual updates.

Why Use Noindex and Nofollow?

  • Privacy and Control: Keeps internal, private, or less important content out of search engine results.
  • SEO Strategy: Helps focus search engine attention on the content you want to rank, avoiding dilution of SEO efforts.
  • Content Management: Provides greater control over how your content is accessed and crawled by search engines.

Whether manually via Yoast SEO’s interface or programmatically through your theme’s functions.php, controlling how search engines interact with your content is a vital aspect of SEO management in WordPress. These methods ensure that your CPTs are indexed and followed according to your specific requirements, contributing to a more refined and strategic SEO approach for your website.

Remember to always back up your site before making changes to theme files or settings.

Leave a Reply

Your email address will not be published. Required fields are marked *