Make WordPress Core


Ignore:
Timestamp:
10/23/2023 03:40:44 PM (3 years ago)
Author:
swissspidy
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/sitemaps/wpSitemapsPosts.php

    r53556 r56985  
    6060                $sitemap_entry = array_shift( $url_list );
    6161
    62                 $this->assertArrayHasKey( 'lastmod', $sitemap_entry );
     62                $this->assertEqualSetsWithIndex(
     63                        array(
     64                                'loc'     => home_url( '/' ),
     65                                'lastmod' => '2000-01-01',
     66                        ),
     67                        $sitemap_entry
     68                );
    6369        }
    6470
     
    6773         */
    6874        public function _show_on_front_entry( $sitemap_entry ) {
    69                 $sitemap_entry['lastmod'] = wp_date( DATE_W3C, time() );
     75                $sitemap_entry['lastmod'] = '2000-01-01';
    7076
    7177                return $sitemap_entry;
     
    94100
    95101                foreach ( $post_ids as $post_id ) {
    96                         $expected[] = array( 'loc' => home_url( "?p={$post_id}" ) );
     102                        $expected[] = array(
     103                                'loc'     => home_url( "?p={$post_id}" ),
     104                                'lastmod' => get_post_modified_time( DATE_W3C, true, $post_id ),
     105                        );
    97106                }
    98107
Note: See TracChangeset for help on using the changeset viewer.