Sitemaps: Ensure entry for ‘page’ post type sitemap in index.

If there are no pages and no static homepage, there will still be one sitemap including the homepage URL.

This change ensures that this sitemap is correctly listed in the sitemap index.

Props Chouby, pacifika, elrae.
Fixes #50571.

git-svn-id: https://develop.svn.wordpress.org/trunk@48476 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
swissspidy 2020-07-14 12:24:02 +00:00
parent 7d5b488b3b
commit 0ee9100e6d
2 changed files with 25 additions and 1 deletions

View File

@ -174,7 +174,8 @@ class WP_Sitemaps_Posts extends WP_Sitemaps_Provider {
$query = new WP_Query( $args ); $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;
} }
/** /**

View File

@ -4,6 +4,29 @@
* @group sitemaps * @group sitemaps
*/ */
class Test_WP_Sitemaps_Posts extends WP_UnitTestCase { 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. * Test ability to filter object subtypes.
*/ */