Sitemap xml feed is shown but 404 header added by WordPress if site has no posts
-
Installed a new site, it has only pages, no posts.
Also installed the WordPress Sitemap Generator Plugin with a modified template for the pages.
Testing
example.com/sitemap.xml
in browser showed the xml with all the pages, all seemed fine.But then Google Webmaster Tools complained about a 404 not found for the
/sitemap.xml
url.Some testing showed that WordPress adds a 404 header to all feeds, including the sitemap xml feed, if no posts exist in the website.
Simple workaround would be adding a post with some content to the site, even if it is not linked anywhere.
A better workaround would override the 404 somehow and after some research I used this code for
functions.php
for now:function my_404_fix() { global $wp_query; if (is_feed() && $wp_query->query_vars['feed'] == 'sitemap') { status_header(200); $wp_query->is_404=false; } } add_filter('template_redirect', 'my_404_fix');
Is there a better solution? Can this behaviour be considered as a bug in WordPress?
- The topic ‘Sitemap xml feed is shown but 404 header added by WordPress if site has no posts’ is closed to new replies.