SEO PHP Beginner

Add Last-Modified Dates to the Sitemap

Add a lastmod date to every post and page in WordPress's sitemap, so search engines know what changed.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 4 lines
add_filter( 'wp_sitemaps_posts_entry', function ( array $entry, WP_Post $post ): array {
	$entry['lastmod'] = get_post_modified_time( 'c', true, $post );
	return $entry;
}, 10, 2 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

WordPress’s built-in sitemap lists URLs but not when they last changed. Search engines use lastmod to decide what to recrawl first, so adding it helps updated content get picked up sooner.

How it works

Each URL in a post type sitemap passes through wp_sitemaps_posts_entry. The snippet adds the post’s modified time in UTC, in the ISO 8601 format sitemaps require. WordPress’s sitemap stylesheet then shows a “Last Modified” column.

Things to watch

  • Google only trusts lastmod if it’s accurate. Don’t change modified dates without real content changes.
  • Taxonomy and user sitemaps aren’t affected, since terms and users have no modified date.

How to test it

Open yoursite.com/wp-sitemap-posts-post-1.xml. Each URL should now show a Last Modified date.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only