is_sitemap(): bool

Is the query for a sitemap?

Return

bool Whether the query is for a sitemap.

Source

function is_sitemap(): bool {
	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.' ), '7.1.0' );
		return false;
	}

	return $wp_query->is_sitemap();
}

Changelog

VersionDescription
7.1.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Introduced 5 years after the sitemaps feature — and doesn’t cover the XSL stylesheet routes

    `is_sitemap() ` was only added in WP 7.1, even though the underlying XML Sitemaps feature (`WP_Sitemaps`) has existed since WP 5.5. Before 7.1, `WP_Query` had no `$is_sitemap` property at all — see core ticket #51117 for the backstory. Adding it also fixed a real bug where sitemap requests could incorrectly be treated as the site’s home page by `WP_Query::parse_query()`, since the old fallback logic for `is_home` didn’t check against sitemap requests.

    Two things worth knowing if you use this function:

    1. Pre-7.1 fallback. If you need to support older WordPress versions, `is_sitemap() ` won’t exist. Detect a sitemap request manually instead:

    if ( function_exists( ‘is_sitemap’ ) ) {
    $is_sitemap_request = is_sitemap() ;
    } else {
    $is_sitemap_request = (bool) get_query_var( ‘sitemap’ );
    }

    2. It does not cover the XSL stylesheet routes. `is_sitemap() ` (and the underlying `$wp_query::is_sitemap() ` property) is only set to `true` when the `sitemap` query var is present — i.e. for the actual XML sitemap index and provider routes (`/wp-sitemap.xml`, `/wp-sitemap-posts-1.xml`, etc.). The stylesheet requests that style those sitemaps in a browser (`/wp-sitemap.xsl` and `/wp-sitemap-index.xsl`) use a separate `sitemap-stylesheet` query var and never set `sitemap`, so `is_sitemap() ` returns `false` for them. If you need to detect those too, check the query var directly:

    $is_sitemap_related = is_sitemap() || (bool) get_query_var( ‘sitemap-stylesheet’ );

You must log in before being able to contribute a note or feedback.