Exclude some posts from WordPress SEO plugin’s sitemaps

Today here is a snippet that allows you to exclude some pages, posts or custom posts from the WordPress SEO plugin generated sitemaps.

Even if this plugin is awesome, some improvements can be done. In my case I needed to exclude two pages from the xml.

The only thing to do is to copy, paste and adapt this code into your functions.php theme file :

add_filter('wpseo_posts_where', 'exclude_posts_from_sitemapyoast');
function exclude_posts_from_sitemapyoast() { 
$where_filter = "AND ID NOT IN (480, 470)"; 
return $where_filter; 

Then you juste have to change the IDs in order to match those you want to exclude from the sitemaps.

This method can be modified in order to provide an UI selection of the posts in the admin or even matching postnames instead of IDs.