Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sitemaps: add lastmod for individual posts and the homepage #4901

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ public function get_url_list( $page_num, $object_subtype = '' ) {
'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'.
*
Expand All @@ -125,7 +147,8 @@ public function get_url_list( $page_num, $object_subtype = '' ) {

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 ) ),
);

/**
Expand Down
8 changes: 6 additions & 2 deletions tests/phpunit/tests/sitemaps/sitemaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,16 @@ public function test_get_url_list_page_with_home() {

$post_list = $providers['posts']->get_url_list( 1, 'page' );

$post_list_sorted = wp_list_sort( $post_list, 'lastmod', 'DESC' );

$expected = $this->_get_expected_url_list( 'page', self::$pages );

// Add the homepage to the front of the URL list.
array_unshift(
$expected,
array(
'loc' => home_url( '/' ),
'loc' => home_url( '/' ),
'lastmod' => $post_list_sorted[0]['lastmod'],
)
);

Expand Down Expand Up @@ -378,7 +381,8 @@ public function _get_expected_url_list( $type, $ids ) {
return array_map(
static function ( $post ) {
return array(
'loc' => get_permalink( $post ),
'loc' => get_permalink( $post ),
'lastmod' => get_post_modified_time( DATE_W3C, true, $post ),
);
},
$posts
Expand Down
15 changes: 12 additions & 3 deletions tests/phpunit/tests/sitemaps/wpSitemapsPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,20 @@ public function test_posts_show_on_front_entry() {
$url_list = $posts_provider->get_url_list( 1, 'page' );
$sitemap_entry = array_shift( $url_list );

$this->assertArrayHasKey( 'lastmod', $sitemap_entry );
$this->assertEqualSetsWithIndex(
array(
'loc' => home_url( '/' ),
'lastmod' => '2000-01-01',
),
$sitemap_entry
);
}

/**
* Callback for 'wp_sitemaps_posts_show_on_front_entry' filter.
*/
public function _show_on_front_entry( $sitemap_entry ) {
$sitemap_entry['lastmod'] = wp_date( DATE_W3C, time() );
$sitemap_entry['lastmod'] = '2000-01-01';

return $sitemap_entry;
}
Expand All @@ -93,7 +99,10 @@ public function test_posts_sticky_posts_not_moved_to_front() {
$expected = array();

foreach ( $post_ids as $post_id ) {
$expected[] = array( 'loc' => home_url( "?p={$post_id}" ) );
$expected[] = array(
'loc' => home_url( "?p={$post_id}" ),
'lastmod' => get_post_modified_time( DATE_W3C, true, $post_id ),
);
}

// Check that the URL list is still in the order of the post IDs (i.e., sticky post wasn't moved to the front).
Expand Down
Loading