From 62dbddcdf4a83cae32d3970bbdbf0b774ce00eda Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 9 Jun 2026 20:58:16 +0200 Subject: [PATCH 1/7] DOING_SITEMAP and is_sitemap --- src/wp-includes/class-wp-query.php | 24 ++++++++++++++++++- src/wp-includes/load.php | 18 ++++++++++++++ src/wp-includes/query.php | 20 ++++++++++++++++ .../sitemaps/class-wp-sitemaps-renderer.php | 8 +++++++ 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 437c82f1dd7f1..3a2177436f558 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -410,6 +410,14 @@ class WP_Query { */ public $is_favicon = false; + /** + * Signifies whether the current query is for a sitemap. + * + * @since 7.1.0 + * @var bool + */ + public $is_sitemap = false; + /** * Signifies whether the current query is for the page_for_posts page. * @@ -519,6 +527,7 @@ private function init_query_flags() { $this->is_singular = false; $this->is_robots = false; $this->is_favicon = false; + $this->is_sitemap = false; $this->is_posts_page = false; $this->is_post_type_archive = false; } @@ -817,6 +826,8 @@ public function parse_query( $query = '' ) { $this->is_robots = true; } elseif ( ! empty( $query_vars['favicon'] ) ) { $this->is_favicon = true; + } elseif ( ! empty( $query_vars['sitemap'] ) || ! empty( $query_vars['sitemap-stylesheet'] ) ) { + $this->is_sitemap = true; } if ( ! is_scalar( $query_vars['p'] ) || (int) $query_vars['p'] < 0 ) { @@ -1040,7 +1051,7 @@ public function parse_query( $query = '' ) { if ( ! ( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || ( wp_is_serving_rest_request() && $this->is_main_query() ) - || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots || $this->is_favicon ) ) { + || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots || $this->is_favicon || $this->is_sitemap ) ) { $this->is_home = true; } @@ -4638,6 +4649,17 @@ public function is_favicon() { return (bool) $this->is_favicon; } + /** + * Determines whether the query is for a sitemap. + * + * @since 7.1.0 + * + * @return bool Whether the query is for a sitemap. + */ + public function is_sitemap() { + return (bool) $this->is_sitemap; + } + /** * Determines whether the query is for a search. * diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php index 27c58b57dd671..33e6c7676f5c4 100644 --- a/src/wp-includes/load.php +++ b/src/wp-includes/load.php @@ -1789,6 +1789,24 @@ function wp_doing_cron() { return apply_filters( 'wp_doing_cron', defined( 'DOING_CRON' ) && DOING_CRON ); } +/** + * Determines whether the current request is a WordPress sitemap request. + * + * @since 7.1.0 + * + * @return bool True if it's a WordPress sitemap request, false otherwise. + */ +function wp_doing_sitemap() { + /** + * Filters whether the current request is a WordPress sitemap request. + * + * @since 7.1.0 + * + * @param bool $wp_doing_sitemap Whether the current request is a WordPress sitemap request. + */ + return apply_filters( 'wp_doing_sitemap', defined( 'DOING_SITEMAP' ) && DOING_SITEMAP ); +} + /** * Checks whether the given variable is a WordPress Error. * diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 592e70e0290a3..c741c574edb4d 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -680,6 +680,26 @@ function is_favicon() { return $wp_query->is_favicon(); } +/** + * Is the query for a sitemap? + * + * @since 7.1.0 + * + * @global WP_Query $wp_query WordPress Query object. + * + * @return bool Whether the query is for a sitemap. + */ +function is_sitemap() { + global $wp_query; + + if ( ! isset( $wp_query ) ) { + _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); + return false; + } + + return $wp_query->is_sitemap(); +} + /** * Determines whether the query is for a search. * diff --git a/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php b/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php index 10a1ef1e7fcd5..232a8c59831ad 100644 --- a/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php +++ b/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php @@ -123,6 +123,10 @@ public function get_sitemap_index_stylesheet_url() { * @param array $sitemaps Array of sitemap URLs. */ public function render_index( $sitemaps ) { + if ( ! defined( 'DOING_SITEMAP' ) ) { + define( 'DOING_SITEMAP', true ); + } + header( 'Content-Type: application/xml; charset=UTF-8' ); $this->check_for_simple_xml_availability(); @@ -187,6 +191,10 @@ public function get_sitemap_index_xml( $sitemaps ) { * @param array $url_list Array of URLs for a sitemap. */ public function render_sitemap( $url_list ) { + if ( ! defined( 'DOING_SITEMAP' ) ) { + define( 'DOING_SITEMAP', true ); + } + header( 'Content-Type: application/xml; charset=UTF-8' ); $this->check_for_simple_xml_availability(); From 78736285f80f319d5dfe7558c54e736142d12596 Mon Sep 17 00:00:00 2001 From: Brian Date: Wed, 10 Jun 2026 15:46:44 +0200 Subject: [PATCH 2/7] fix wrong version --- src/wp-includes/query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index c741c574edb4d..519f639b185f2 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -693,7 +693,7 @@ function is_sitemap() { global $wp_query; if ( ! isset( $wp_query ) ) { - _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); + _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '7.1.0' ); return false; } From 8a1c8de99c04fbf074ce60540d325a9286f1ec8b Mon Sep 17 00:00:00 2001 From: Brian Date: Wed, 10 Jun 2026 15:48:19 +0200 Subject: [PATCH 3/7] remove sitemap-stylesheet condition --- src/wp-includes/class-wp-query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 3a2177436f558..064644bd72785 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -826,7 +826,7 @@ public function parse_query( $query = '' ) { $this->is_robots = true; } elseif ( ! empty( $query_vars['favicon'] ) ) { $this->is_favicon = true; - } elseif ( ! empty( $query_vars['sitemap'] ) || ! empty( $query_vars['sitemap-stylesheet'] ) ) { + } elseif ( ! empty( $query_vars['sitemap'] ) ) { $this->is_sitemap = true; } From 77ddc19ef7870721b4c3889b3fe226a7c581c40b Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 11 Jun 2026 08:18:25 +0200 Subject: [PATCH 4/7] remove changes for DOING_SITEMAP to seperate PR --- src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php b/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php index 232a8c59831ad..10a1ef1e7fcd5 100644 --- a/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php +++ b/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php @@ -123,10 +123,6 @@ public function get_sitemap_index_stylesheet_url() { * @param array $sitemaps Array of sitemap URLs. */ public function render_index( $sitemaps ) { - if ( ! defined( 'DOING_SITEMAP' ) ) { - define( 'DOING_SITEMAP', true ); - } - header( 'Content-Type: application/xml; charset=UTF-8' ); $this->check_for_simple_xml_availability(); @@ -191,10 +187,6 @@ public function get_sitemap_index_xml( $sitemaps ) { * @param array $url_list Array of URLs for a sitemap. */ public function render_sitemap( $url_list ) { - if ( ! defined( 'DOING_SITEMAP' ) ) { - define( 'DOING_SITEMAP', true ); - } - header( 'Content-Type: application/xml; charset=UTF-8' ); $this->check_for_simple_xml_availability(); From 7058b0284cd67ac12805e077790701c31cc31a7f Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 11 Jun 2026 08:19:01 +0200 Subject: [PATCH 5/7] remove changes for DOING_SITEMAP to seperate PR --- src/wp-includes/load.php | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php index 33e6c7676f5c4..27c58b57dd671 100644 --- a/src/wp-includes/load.php +++ b/src/wp-includes/load.php @@ -1789,24 +1789,6 @@ function wp_doing_cron() { return apply_filters( 'wp_doing_cron', defined( 'DOING_CRON' ) && DOING_CRON ); } -/** - * Determines whether the current request is a WordPress sitemap request. - * - * @since 7.1.0 - * - * @return bool True if it's a WordPress sitemap request, false otherwise. - */ -function wp_doing_sitemap() { - /** - * Filters whether the current request is a WordPress sitemap request. - * - * @since 7.1.0 - * - * @param bool $wp_doing_sitemap Whether the current request is a WordPress sitemap request. - */ - return apply_filters( 'wp_doing_sitemap', defined( 'DOING_SITEMAP' ) && DOING_SITEMAP ); -} - /** * Checks whether the given variable is a WordPress Error. * From 6bb5700e56d6ac596771ebce220153fe43c0dd91 Mon Sep 17 00:00:00 2001 From: Brian Date: Mon, 15 Jun 2026 19:28:09 +0200 Subject: [PATCH 6/7] Apply suggestions from code review Co-authored-by: Weston Ruter --- src/wp-includes/class-wp-query.php | 7 +++---- src/wp-includes/query.php | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 064644bd72785..f9c7f090bc753 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -414,9 +414,8 @@ class WP_Query { * Signifies whether the current query is for a sitemap. * * @since 7.1.0 - * @var bool */ - public $is_sitemap = false; + public bool $is_sitemap = false; /** * Signifies whether the current query is for the page_for_posts page. @@ -4656,8 +4655,8 @@ public function is_favicon() { * * @return bool Whether the query is for a sitemap. */ - public function is_sitemap() { - return (bool) $this->is_sitemap; + public function is_sitemap(): bool { + return $this->is_sitemap; } /** diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 519f639b185f2..60571c01cb880 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -689,7 +689,7 @@ function is_favicon() { * * @return bool Whether the query is for a sitemap. */ -function is_sitemap() { +function is_sitemap(): bool { global $wp_query; if ( ! isset( $wp_query ) ) { From 9d83b9a342b5e25d15fbdd3e6586dbb521fd1510 Mon Sep 17 00:00:00 2001 From: Nimesh Date: Tue, 16 Jun 2026 11:51:49 +0530 Subject: [PATCH 7/7] Tests: Add unit tests for the is_sitemap() conditional tag. Adds test coverage for the is_sitemap() conditional query tag and the WP_Query::$is_sitemap property introduced for #51543. Tests cover the WP_Query property default, the sitemap index and subtype routes, the sitemap stylesheet route exclusion, is_robots() precedence, the guarantee that a sitemap request is not treated as the home/front page, the global is_sitemap() conditional tag, and the _doing_it_wrong() notice when called before the query is run. Also adds is_sitemap to WP_UnitTestCase::assertQueryTrue() alongside the existing is_robots and is_favicon conditionals for consistency. See #51543. --- tests/phpunit/includes/abstract-testcase.php | 1 + tests/phpunit/tests/query/isSitemap.php | 213 +++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 tests/phpunit/tests/query/isSitemap.php diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index b8e8598362ec5..55a9924fb23c3 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -1187,6 +1187,7 @@ public function assertQueryTrue( ...$prop ) { 'is_preview', 'is_robots', 'is_favicon', + 'is_sitemap', 'is_search', 'is_single', 'is_singular', diff --git a/tests/phpunit/tests/query/isSitemap.php b/tests/phpunit/tests/query/isSitemap.php new file mode 100644 index 0000000000000..004aa1ed86455 --- /dev/null +++ b/tests/phpunit/tests/query/isSitemap.php @@ -0,0 +1,213 @@ +post->create_many( 3 ); + } + + public function set_up() { + parent::set_up(); + + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + + create_initial_taxonomies(); + } + + /** + * The property defaults to false on a freshly initialized query. + * + * @covers WP_Query::init + */ + public function test_is_sitemap_defaults_to_false() { + $query = new WP_Query(); + + $this->assertFalse( $query->is_sitemap, 'The $is_sitemap property should default to false.' ); + $this->assertFalse( $query->is_sitemap(), 'WP_Query::is_sitemap() should return false by default.' ); + } + + /** + * The flag is set when the "sitemap" query var is present (sitemap index route). + * + * @covers WP_Query::parse_query + * @covers WP_Query::is_sitemap + */ + public function test_is_sitemap_true_for_sitemap_index() { + $query = new WP_Query( array( 'sitemap' => 'index' ) ); + + $this->assertTrue( $query->is_sitemap, 'The $is_sitemap property should be true for a sitemap query.' ); + $this->assertTrue( $query->is_sitemap(), 'WP_Query::is_sitemap() should return true for a sitemap query.' ); + } + + /** + * The flag is set for a sitemap subtype route (e.g. wp-sitemap-posts-post-1.xml). + * + * @covers WP_Query::parse_query + * @covers WP_Query::is_sitemap + */ + public function test_is_sitemap_true_for_sitemap_subtype() { + $query = new WP_Query( + array( + 'sitemap' => 'posts', + 'sitemap-subtype' => 'post', + 'paged' => 1, + ) + ); + + $this->assertTrue( $query->is_sitemap(), 'WP_Query::is_sitemap() should return true for a sitemap subtype query.' ); + } + + /** + * is_sitemap() must return a boolean. + * + * @covers WP_Query::is_sitemap + */ + public function test_is_sitemap_return_type_is_bool() { + $query = new WP_Query( array( 'sitemap' => 'index' ) ); + + $this->assertIsBool( $query->is_sitemap(), 'WP_Query::is_sitemap() should return a boolean.' ); + } + + /** + * An empty "sitemap" query var must not set the flag. + * + * @covers WP_Query::parse_query + */ + public function test_is_sitemap_false_for_empty_sitemap_var() { + $query = new WP_Query( array( 'sitemap' => '' ) ); + + $this->assertFalse( $query->is_sitemap(), 'An empty "sitemap" query var should not set is_sitemap.' ); + } + + /** + * The sitemap stylesheet route uses the "sitemap-stylesheet" query var, which must + * not flag the query as a sitemap. + * + * @covers WP_Query::parse_query + */ + public function test_is_sitemap_false_for_stylesheet_route() { + $query = new WP_Query( array( 'sitemap-stylesheet' => 'sitemap' ) ); + + $this->assertFalse( $query->is_sitemap(), 'The sitemap stylesheet route should not flag the query as a sitemap.' ); + } + + /** + * is_robots takes precedence over is_sitemap in the parse_query branch. + * + * @covers WP_Query::parse_query + */ + public function test_robots_takes_precedence_over_sitemap() { + $query = new WP_Query( + array( + 'robots' => true, + 'sitemap' => 'index', + ) + ); + + $this->assertTrue( $query->is_robots(), 'is_robots() should be true when the robots query var is set.' ); + $this->assertFalse( $query->is_sitemap(), 'is_sitemap() should be false when is_robots() takes precedence.' ); + } + + /** + * A regular query is never flagged as a sitemap. + * + * @covers WP_Query::is_sitemap + */ + public function test_is_sitemap_false_for_regular_query() { + $post_id = self::factory()->post->create(); + + $query = new WP_Query( array( 'p' => $post_id ) ); + + $this->assertFalse( $query->is_sitemap(), 'A regular post query should not be flagged as a sitemap.' ); + } + + /** + * A sitemap query must not also be treated as the home/front page. + * + * This is the practical motivation for the conditional tag: distinguishing a + * sitemap request from the home page (see #51542). + * + * @covers WP_Query::parse_query + */ + public function test_sitemap_query_is_not_home() { + $query = new WP_Query( array( 'sitemap' => 'index' ) ); + + $this->assertTrue( $query->is_sitemap(), 'The sitemap query should be flagged as a sitemap.' ); + $this->assertFalse( $query->is_home(), 'A sitemap query should not be treated as the home page.' ); + $this->assertFalse( $query->is_front_page(), 'A sitemap query should not be treated as the front page.' ); + } + + /** + * The global is_sitemap() conditional tag reflects the main query. + * + * @covers ::is_sitemap + */ + public function test_global_is_sitemap_reflects_main_query() { + // Prevent WP_Sitemaps from rendering and calling exit during go_to(). + remove_action( 'template_redirect', array( wp_sitemaps_get_server(), 'render_sitemaps' ) ); + + $this->go_to( home_url( '/?sitemap=index' ) ); + + $this->assertTrue( is_sitemap(), 'is_sitemap() should be true on a sitemap request.' ); + + // is_sitemap should be the only conditional that is true for a sitemap request. + $this->assertQueryTrue( 'is_sitemap' ); + } + + /** + * The global is_sitemap() conditional tag is false for a non-sitemap request. + * + * @covers ::is_sitemap + */ + public function test_global_is_sitemap_false_on_home() { + $this->go_to( home_url( '/' ) ); + + $this->assertFalse( is_sitemap(), 'is_sitemap() should be false on the home page.' ); + $this->assertTrue( is_home(), 'is_home() should be true on the home page.' ); + } + + /** + * The global is_sitemap() returns false and triggers a notice when the query + * has not yet run. + * + * @covers ::is_sitemap + * + * @expectedIncorrectUsage is_sitemap + */ + public function test_global_is_sitemap_before_query_is_run() { + $wp_query_temp = $GLOBALS['wp_query']; + unset( $GLOBALS['wp_query'] ); + + $result = is_sitemap(); + + $GLOBALS['wp_query'] = $wp_query_temp; + + $this->assertFalse( $result, 'is_sitemap() should return false before the query is run.' ); + } +}