From 1555e715f62b3cf91d519d10e624faacfac23273 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 23 Oct 2023 15:42:22 +0000 Subject: [PATCH] Sitemaps: add `lastmod` for individual posts and the homepage. When the XML sitemaps feature was originally introduced, the `lastmod` field was omitted because guidance at the time indicated it was less important for search engines, plus for some entities it was computationally expensive to add. Now that the guidance has slightly changed, we are revisiting this and adding `lastmod` where easily possible. - Adds `lastmod` to all individual post objects (of any post type) in the sitemap - Adds `lastmod` to the homepage sitemap entry if the homepage is set to display the latest posts. No `lastmod` is added for the individual sitemap pages in the sitemap index, nor for term archives or user archives. Those enhancements require additional changes, such as storing the modified date for a taxonomy term when something is added to that term. They can be revisited in separate follow-up tickets. Props swissspidy, joemcgill. Fixes #52099 Built from https://develop.svn.wordpress.org/trunk@56985 git-svn-id: http://core.svn.wordpress.org/trunk@56496 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../providers/class-wp-sitemaps-posts.php | 25 ++++++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php b/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php index dff85a70e1..581b4ca2a3 100644 --- a/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php +++ b/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php @@ -112,6 +112,28 @@ class WP_Sitemaps_Posts extends WP_Sitemaps_Provider { 'loc' => home_url( '/' ), ); + /* + * Get the most recent posts displayed on the homepage, + * and then sort them by their modified date to find + * the date the homepage was approximately last updated. + */ + $latest_posts = new WP_Query( + array( + 'post_type' => 'post', + 'post_status' => 'publish', + 'orderby' => 'date', + 'order' => 'DESC', + 'no_found_rows' => true, + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + ) + ); + + if ( ! empty( $latest_posts->posts ) ) { + $posts = wp_list_sort( $latest_posts->posts, 'post_modified_gmt', 'DESC' ); + $sitemap_entry['lastmod'] = wp_date( DATE_W3C, strtotime( $posts[0]->post_modified_gmt ) ); + } + /** * Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'. * @@ -125,7 +147,8 @@ class WP_Sitemaps_Posts extends WP_Sitemaps_Provider { foreach ( $query->posts as $post ) { $sitemap_entry = array( - 'loc' => get_permalink( $post ), + 'loc' => get_permalink( $post ), + 'lastmod' => wp_date( DATE_W3C, strtotime( $post->post_modified_gmt ) ), ); /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 32f7aa7153..2af4fa3d0a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-alpha-56984'; +$wp_version = '6.5-alpha-56985'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.