[wp-trac] [WordPress Trac] #51117: Sitemap & XSL requests execute main query for home page.

WordPress Trac noreply at wordpress.org
Thu May 4 23:03:53 UTC 2023


#51117: Sitemap & XSL requests execute main query for home page.
---------------------------+------------------------------
 Reporter:  peterwilsoncc  |       Owner:  pbiron
     Type:  defect (bug)   |      Status:  accepted
 Priority:  normal         |   Milestone:  Awaiting Review
Component:  Sitemaps       |     Version:  5.5
 Severity:  normal         |  Resolution:
 Keywords:  needs-patch    |     Focuses:  performance
---------------------------+------------------------------

Comment (by RavanH):

 Currently using this as a patch (via plugin) solving all issues. The
 functions `is_sitemap()` and `is_sitemap_stylesheet()` are not technically
 needed in this context but seem logical to have available (and use)...

 {{{
 add_action( 'parse_request', 'wp_sitemaps_loaded' );

 if ( ! function_exists( 'wp_sitemaps_loaded' ) ) :
         /**
          * Loads the WordPress XML Sitemap Server
          *
          * @see https://core.trac.wordpress.org/ticket/51912
          *
          * @since 1.0
          *
          * @param  WP       $wp       Current WordPress environment
 instance.
          * @global WP_Query     $wp_query WordPress Query.
          * @return void
          */
         function wp_sitemaps_loaded( $wp ) {
                 global $wp_query;

                 /**
                  * Whether this is a Sitemap Request.
                  *
                  * @see https://core.trac.wordpress.org/ticket/51543
                  * @since 1.0
                  * @var bool
                  */
                 $wp_query->is_sitemap = ! empty(
 $wp->query_vars['sitemap'] );

                 /**
                  * Whether this is a Sitemap Stylesheet Request.
                  *
                  * @since 1.0
                  * @var bool
                  */
                 $wp_query->is_sitemap_stylesheet = ! empty(
 $wp->query_vars['sitemap-stylesheet'] );

                 if ( ! is_sitemap() && ! is_sitemap_stylesheet() ) {
                         return;
                 }

                 // Prepare query variables.
                 $query_vars = $wp_query->query_vars;
                 $wp_query->query_vars = $wp->query_vars;

                 // Render the sitemap.
                 wp_sitemaps_get_server()->render_sitemaps();

                 // Still here? Then it was an invalid sitemap request
 after all. Undo everything and carry on...
                 $wp_query->is_sitemap = false;
                 $wp_query->is_sitemap_stylesheet = false;
                 $wp_query->query_vars = $query_vars;
         }
 endif;

 if ( ! function_exists( 'is_sitemap' ) ) :
         /**
          * Determines whether the query is for the sitemap.
          *
          * For more information on this and similar theme functions, check
 out
          * the {@link https://developer.wordpress.org/themes/basics
 /conditional-tags/
          * Conditional Tags} article in the Theme Developer Handbook.
          *
          * @see https://core.trac.wordpress.org/ticket/51543
          *
          * @since 1.0
          *
          * @global WP_Query $wp_query WordPress Query object.
          * @return bool Whether the query is for the sitemap.
          */
         function is_sitemap() {
                 global $wp_query;

                 if ( ! isset( $wp_query ) ) {
                         _doing_it_wrong( __FUNCTION__, translate(
 'Conditional query tags do not work before the query is run. Before then,
 they always return false.' ), '3.1.0' );
                         return false;
                 }

                 return property_exists( $wp_query, 'is_sitemap' ) ?
 $wp_query->is_sitemap : false;
         }
 endif;

 if ( ! function_exists( 'is_sitemap_stylesheet' ) ) :
         /**
          * Determines whether the query is for the sitemap stylesheet.
          *
          * For more information on this and similar theme functions, check
 out
          * the {@link https://developer.wordpress.org/themes/basics
 /conditional-tags/
          * Conditional Tags} article in the Theme Developer Handbook.
          *
          * @since 1.0
          *
          * @global WP_Query $wp_query WordPress Query object.
          * @return bool Whether the query is for the sitemap stylesheet.
          */
         function is_sitemap_stylesheet() {
                 global $wp_query;

                 if ( ! isset( $wp_query ) ) {
                         _doing_it_wrong( __FUNCTION__, translate(
 'Conditional query tags do not work before the query is run. Before then,
 they always return false.' ), '3.1.0' );
                         return false;
                 }

                 return property_exists( $wp_query, 'is_sitemap_stylesheet'
 ) ? $wp_query->is_sitemap_stylesheet : false;
         }
 endif;

 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51117#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list