Leave Old Posts Out of the All in One SEO Sitemap
Removes post entries that have not been modified for several years from All in One SEO's post sitemap.
Opens WPIDE on
Code
// Entries not modified for this many years are dropped.
$wpide_aioseo_max_age = 3;
add_filter( 'aioseo_sitemap_posts', function ( $entries, $post_type ) use ( $wpide_aioseo_max_age ) {
if ( 'post' !== $post_type || ! is_array( $entries ) ) {
return $entries;
}
$cutoff = strtotime( '-' . absint( $wpide_aioseo_max_age ) . ' years' );
return array_values( array_filter( $entries, function ( $entry ) use ( $cutoff ) {
$time = isset( $entry['lastmod'] ) ? strtotime( (string) $entry['lastmod'] ) : false;
return ! $time || $time >= $cutoff;
} ) );
}, 10, 2 );
Notes
Only the "post" type is filtered; pages and other types are kept.
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.