Leave Old Posts Out of the Yoast Sitemap
Removes posts that have not been updated for a number of years from the Yoast SEO post sitemap, so crawlers spend time on fresher content.
Opens WPIDE on
Code
// Posts not modified for this many years are left out.
$wpide_sitemap_max_age_years = 3;
add_filter( 'wpseo_sitemap_entry', function ( $url, $type, $object ) use ( $wpide_sitemap_max_age_years ) {
if ( 'post' !== $type || ! $object instanceof WP_Post || 'post' !== $object->post_type ) {
return $url;
}
$cutoff = strtotime( '-' . absint( $wpide_sitemap_max_age_years ) . ' years' );
return strtotime( $object->post_modified_gmt . ' UTC' ) < $cutoff ? false : $url;
}, 10, 3 );
Notes
The posts stay published and indexable; only the sitemap entry goes.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.