Update:
Okay, this was relatively easy to solve with the function below:
/*
* no-index posts that are a specific post format
*/
add_filter(
'wp_robots',
function(array $robots) {
if ( has_post_format( 'link' )) {
return array_merge(
$robots,
array(
'noindex' => true,
'nofollow' => true,
)
);
} else {
return $robots;
}
}
);
But this leaves another issue. If these are automatically no-indexed they also need to be removed from the sitemap too, otherwise we will have an SEO issue of submitting no-indexed posts in our sitemap.
I tried to modify a filter from https://yoast.com/help/sitemap-shows-excluded-posts-pages/ to create:
/*
* Remove post format from Sitemap
*/
function sitemap_exclude_post_format( $excluded, $post_format ) {
return $post_format === 'link';
}
add_filter( 'wpseo_sitemap_exclude_post_format', 'sitemap_exclude_post_type', 10, 2 );
Unfortunately this doesn’t seem to work.
I just saw https://ww.wp.xz.cn/support/topic/exclude-post-by-format-in-sitemap/ but I’m curious why this hasn’t been added? Or has it been added since? If not, I think it’s pretty essential to provide an easy way to use any conditional statement to alter what is in the XML sitemap.
Please advise.
Hello @one3rdnerd
Thanks for using the Yoast SEO plugin. There’s no feature in the plugin or a developer filter to noindex posts based on their format. You could create a feature request, though, for our product team to review.
Hi @maybellyne
Maybe you missed my second comment but I already resolved this with an existing filter and using a conditional statement. See the code I pasted above.
My issue now is that I need to remove posts from the sitemap.xml based on the same conditional but that’s an oversight in how the filters have been created for Sitemaps where we can’t achieve that.
I think it’s pretty essential that this is fixed in Yoast because it makes the noindexing by conditional statements sort of redundant.
I have added this as a feature request but I’m going to consider deactivating Yoast and trying to achieve this with another SEO plugin instead if it’s not something that can be fixed relatively quickly.
Here’s the feature request on GitHub for anyone who wants to comment or upvote it.
https://github.com/Yoast/wordpress-seo/issues/20437
Thanks
Thanks for opening the feature request. It’d be reviewed shortly by our product team.
-
This reply was modified 2 years, 11 months ago by
Maybellyne.