Make WordPress Core

Changeset 62664


Ignore:
Timestamp:
07/08/2026 07:03:59 AM (5 weeks ago)
Author:
westonruter
Message:

Sitemaps: Add the is_sitemap() conditional tag.

Introduce is_sitemap() as a conditional tag, mirroring the existing is_robots() and is_favicon() tags, and add the corresponding $is_sitemap property to WP_Query. The flag is set whenever the sitemap query variable is present, and a sitemap request is now excluded from being treated as the home page.

Developed in https://github.com/WordPress/wordpress-develop/pull/12142.
Follow-up to r47018, r48072.

Props masteradhoc, nimeshatxecurify, westonruter, mukesh27, madtownlems, ravanh, cybr, swissspidy.
See #51117.
Fixes #51542, #51543.

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r62648 r62664  
    410410         */
    411411        public $is_favicon = false;
     412
     413        /**
     414         * Signifies whether the current query is for a sitemap.
     415         *
     416         * @since 7.1.0
     417         */
     418        public bool $is_sitemap = false;
    412419
    413420        /**
     
    520527                $this->is_robots            = false;
    521528                $this->is_favicon           = false;
     529                $this->is_sitemap           = false;
    522530                $this->is_posts_page        = false;
    523531                $this->is_post_type_archive = false;
     
    818826                } elseif ( ! empty( $query_vars['favicon'] ) ) {
    819827                        $this->is_favicon = true;
     828                } elseif ( ! empty( $query_vars['sitemap'] ) ) {
     829                        $this->is_sitemap = true;
    820830                }
    821831
     
    10411051                if ( ! ( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed
    10421052                                || ( wp_is_serving_rest_request() && $this->is_main_query() )
    1043                                 || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots || $this->is_favicon ) ) {
     1053                                || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots || $this->is_favicon || $this->is_sitemap ) ) {
    10441054                        $this->is_home = true;
    10451055                }
     
    46444654
    46454655        /**
     4656         * Determines whether the query is for a sitemap.
     4657         *
     4658         * @since 7.1.0
     4659         *
     4660         * @return bool Whether the query is for a sitemap.
     4661         */
     4662        public function is_sitemap(): bool {
     4663                return $this->is_sitemap;
     4664        }
     4665
     4666        /**
    46464667         * Determines whether the query is for a search.
    46474668         *
  • trunk/src/wp-includes/query.php

    r60697 r62664  
    679679
    680680        return $wp_query->is_favicon();
     681}
     682
     683/**
     684 * Is the query for a sitemap?
     685 *
     686 * @since 7.1.0
     687 *
     688 * @global WP_Query $wp_query WordPress Query object.
     689 *
     690 * @return bool Whether the query is for a sitemap.
     691 */
     692function is_sitemap(): bool {
     693        global $wp_query;
     694
     695        if ( ! isset( $wp_query ) ) {
     696                _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '7.1.0' );
     697                return false;
     698        }
     699
     700        return $wp_query->is_sitemap();
    681701}
    682702
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r62368 r62664  
    11881188                        'is_robots',
    11891189                        'is_favicon',
     1190                        'is_sitemap',
    11901191                        'is_search',
    11911192                        'is_single',
Note: See TracChangeset for help on using the changeset viewer.