Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.