I’m using Yoast SEO plugin and I’m trying to exclude posts manually from posts XML sitemap using ‘wpseo_sitemap_entry’ filter but so far have no luck.
This is my current code:
function sitemap_exclude_post( $url, $type, $post) {
if($post->ID == 6298 ) {
return false;
}
return $url;
}
add_filter( 'wpseo_sitemap_entry', 'sitemap_exclude_post', 1, 3 );
Any suggestions will be much appreciated?
Note: I know it can be done via Yoast plugin’s backend by manually entering post IDs but I need to do it via filters. The above code is going to be further changed later to automatically obtain Post IDs of posts(single.php) from a category in wordpress.
4
Answers
The above code which I posted in my question works fine.
I just pressed the "save changes" button inside Yoast’s dashboard "Exclude Posts" section and it worked. Not sure how, but it's working.
You can easily do it from Yoast admin interface:
https://kb.yoast.com/kb/sitemap-shows-excluded-posts-pages/
Cheers,
Francesco
Can use update_option function.
Should be work also inside add_filter hook. update_option is a wp function that insert or update inside table options. The key inside wp_options table of Yoast SEO for XML Sitemap is wpseo_xml.
In the value there is an array with other information:
}
I know the question has been answered already but I needed a more dynamical approach of what @clodclod91 explained and since I received the question more than once, I wrote some code about how to do this best in a ‘more fluid way’ than hardcoding any values.
I hope it helps some people.
I also wrote a post with a bit more explanation. Read it here.