Make WordPress Core

Changeset 56985


Ignore:
Timestamp:
10/23/2023 03:40:44 PM (14 months 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

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php

    r54855 r56985  
    113113            );
    114114
     115            /*
     116             * Get the most recent posts displayed on the homepage,
     117             * and then sort them by their modified date to find
     118             * the date the homepage was approximately last updated.
     119             */
     120            $latest_posts = new WP_Query(
     121                array(
     122                    'post_type'              => 'post',
     123                    'post_status'            => 'publish',
     124                    'orderby'                => 'date',
     125                    'order'                  => 'DESC',
     126                    'no_found_rows'          => true,
     127                    'update_post_meta_cache' => false,
     128                    'update_post_term_cache' => false,
     129                )
     130            );
     131
     132            if ( ! empty( $latest_posts->posts ) ) {
     133                $posts = wp_list_sort( $latest_posts->posts, 'post_modified_gmt', 'DESC' );
     134                $sitemap_entry['lastmod'] = wp_date( DATE_W3C, strtotime( $posts[0]->post_modified_gmt ) );
     135            }
     136
    115137            /**
    116138             * Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'.
     
    126148        foreach ( $query->posts as $post ) {
    127149            $sitemap_entry = array(
    128                 'loc' => get_permalink( $post ),
     150                'loc'     => get_permalink( $post ),
     151                'lastmod' => wp_date( DATE_W3C, strtotime( $post->post_modified_gmt ) ),
    129152            );
    130153
  • trunk/tests/phpunit/tests/sitemaps/sitemaps.php

    r51492 r56985  
    252252        $post_list = $providers['posts']->get_url_list( 1, 'page' );
    253253
     254        $post_list_sorted = wp_list_sort( $post_list, 'lastmod', 'DESC' );
     255
    254256        $expected = $this->_get_expected_url_list( 'page', self::$pages );
    255257
     
    258260            $expected,
    259261            array(
    260                 'loc' => home_url( '/' ),
     262                'loc'     => home_url( '/' ),
     263                'lastmod' => $post_list_sorted[0]['lastmod'],
    261264            )
    262265        );
     
    379382            static function ( $post ) {
    380383                return array(
    381                     'loc' => get_permalink( $post ),
     384                    'loc'     => get_permalink( $post ),
     385                    'lastmod' => get_post_modified_time( DATE_W3C, true, $post ),
    382386                );
    383387            },
  • 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.