[wp-trac] [WordPress Trac] #51912: Sitemap pages 404 with more than one page
WordPress Trac
noreply at wordpress.org
Sat May 15 11:21:05 UTC 2021
#51912: Sitemap pages 404 with more than one page
--------------------------+------------------------------
Reporter: loranrendel | Owner: (none)
Type: defect (bug) | Status: closed
Priority: normal | Milestone: Awaiting Review
Component: Sitemaps | Version: 5.5
Severity: normal | Resolution: invalid
Keywords: has-patch | Focuses:
--------------------------+------------------------------
Changes (by tigerfinch):
* keywords: => has-patch
* status: new => closed
* resolution: => invalid
Comment:
(Apologies if I'm tagging this wrong... I've tagged ''has-patch'' as I've
got a potential resolution)
The solution is – when a sitemap is being generated – to force the main
query to use the post_type of the sitemap.
As an **interim solution for users**, I found this worked in theme/plugin
code:
{{{#!php
add_filter('pre_get_posts', function($query) {
global $wp_query;
if ($wp_query->query['sitemap'] === 'posts')
$query->set('post_type', $wp_query->query['sitemap-subtype']);
return $query;
});
}}}
As a **core fix** for this, we could alter the function function
register_rewrites() in class-wp-sitemaps.php to pass the requested custom
post type to the main query:
{{{#!php
// Register routes for providers.
add_rewrite_rule(
'^wp-sitemap-([a-z]+?)-([a-z\d_-]+?)-(\d+?)\.xml$',
'index.php?sitemap=$matches[1]&sitemap-
subtype=$matches[2]&paged=$matches[3]',
'top'
);
}}}
to
{{{#!php
// Register routes for providers.
add_rewrite_rule(
'^wp-sitemap-([a-z]+?)-([a-z\d_-]+?)-(\d+?)\.xml$',
'index.php?sitemap=$matches[1]&sitemap-
subtype=$matches[2]&post_type=$matches[2]&paged=$matches[3]',
'top'
);
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/51912#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list