Set noindex nofollow on posts from specific category using Yoast WordPress SEO

Using the Yoast WordPress SEO filter wpseo_robots filter you can set posts from a specific category to have the meta tag set with noindex/nofollow.

add_filter( 'wpseo_robots', 'yoast_seo_robots_remove_single' );
/**
 * Set certain posts to noindex nofollow
 */
function yoast_seo_robots_remove_single( $robots ) {

	if ( is_single() && has_term( 'uncategorized', 'category' ) ) { 
	return 'noindex,nofollow'; 
} 

else {
	return $robots; 
     }
}

So in the above snippet, any posts with a category of uncategorized will be set to noindex nofollow.

You can change the code to work in any post types, below shows how you can set some WooCommerce products set to noindex/nofollow.

add_filter( 'wpseo_robots', 'yoast_seo_robots_remove_single' );
/**
 * Set certain posts to noindex nofollow
 */
function yoast_seo_robots_remove_single( $robots ) {

	if ( is_product() && has_term( 'pigments', 'product_cat' ) ) { 
	return 'noindex,nofollow'; 
} 

else {
	return $robots; 
     }
}

So in the above snippet, any products with a category of pigments will be set to noindex nofollow.

Double check in the code by searching for noindex and you should see the below…

<meta name="robots" content="max-image-preview:large, noindex,nofollow">