wp/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
--- a/wp/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php	Fri Sep 05 18:40:08 2025 +0200
@@ -112,6 +112,29 @@
 				'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 +148,8 @@
 
 		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 ) ),
 			);
 
 			/**
@@ -193,6 +217,7 @@
 	 * Returns the query args for retrieving posts to list in the sitemap.
 	 *
 	 * @since 5.5.0
+	 * @since 6.1.0 Added `ignore_sticky_posts` default parameter.
 	 *
 	 * @param string $post_type Post type name.
 	 * @return array Array of WP_Query arguments.
@@ -204,6 +229,7 @@
 		 * @see WP_Query for a full list of arguments.
 		 *
 		 * @since 5.5.0
+		 * @since 6.1.0 Added `ignore_sticky_posts` default parameter.
 		 *
 		 * @param array  $args      Array of WP_Query arguments.
 		 * @param string $post_type Post type name.
@@ -219,6 +245,7 @@
 				'no_found_rows'          => true,
 				'update_post_term_cache' => false,
 				'update_post_meta_cache' => false,
+				'ignore_sticky_posts'    => true, // Sticky posts will still appear, but they won't be moved to the front.
 			),
 			$post_type
 		);