diff --git a/src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php b/src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php index e03af6e357..56acfb7697 100644 --- a/src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php +++ b/src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php @@ -174,7 +174,8 @@ class WP_Sitemaps_Posts extends WP_Sitemaps_Provider { $query = new WP_Query( $args ); - return isset( $query->max_num_pages ) ? $query->max_num_pages : 1; + $min_num_pages = ( 'page' === $post_type && 'posts' === get_option( 'show_on_front' ) ) ? 1 : 0; + return isset( $query->max_num_pages ) ? max( $min_num_pages, $query->max_num_pages ) : 1; } /** diff --git a/tests/phpunit/tests/sitemaps/sitemaps-posts.php b/tests/phpunit/tests/sitemaps/sitemaps-posts.php index 8d173942f4..d102518587 100644 --- a/tests/phpunit/tests/sitemaps/sitemaps-posts.php +++ b/tests/phpunit/tests/sitemaps/sitemaps-posts.php @@ -4,6 +4,29 @@ * @group sitemaps */ class Test_WP_Sitemaps_Posts extends WP_UnitTestCase { + /** + * Tests getting sitemap entries for post type page with 'posts' homepage. + * + * Ensures that an entry is added even if there are no pages. + * + * @ticket 50571 + */ + public function test_get_sitemap_entries_homepage() { + update_option( 'show_on_front', 'posts' ); + + $posts_provider = new WP_Sitemaps_Posts(); + + $post_list = $posts_provider->get_sitemap_entries(); + + $expected = array( + array( + 'loc' => home_url( '/?sitemap=posts&sitemap-subtype=page&paged=1' ), + ), + ); + + $this->assertEquals( $expected, $post_list ); + } + /** * Test ability to filter object subtypes. */