Skip to content

Sitemaps: Don't return a 404 status for valid sitemap requests when the main query has no posts - #13247

Open
i-am-chitti wants to merge 1 commit into
WordPress:trunkfrom
i-am-chitti:fix/65945-sitemap-404-no-posts
Open

Sitemaps: Don't return a 404 status for valid sitemap requests when the main query has no posts#13247
i-am-chitti wants to merge 1 commit into
WordPress:trunkfrom
i-am-chitti:fix/65945-sitemap-404-no-posts

Conversation

@i-am-chitti

Copy link
Copy Markdown

Ticket

Trac ticket: https://core.trac.wordpress.org/ticket/65945

Summary

  • On a site with zero published posts, /wp-sitemap.xml renders correct XML but responds with HTTP 404, so search engines discard it.
  • WP::handle_404() 404s the main query when it has no posts and no exception applies. Sitemap requests aren't in that list — they were only shielded by falling through to is_home = true, which changeset 62664 (#51542, #51543) correctly removed.
  • Same class of issue as #39157, fixed for feeds in 4.7.3.

Changes

  1. WP::handle_404() — exempt sitemaps alongside the other machine-readable endpoints. Placed in the first branch rather than next to is_feed(), since provider routes map paged from the URL and so never reach the elseif ( ! is_paged() ) branch.
  2. WP_Sitemaps::render_sitemaps() — 404 explicitly for an unregistered provider, which previously relied on handle_404() doing it.

Intentional 404s are unaffected — sitemaps disabled and empty URL lists (#61293) call set_404() directly, later in the request.

Test

New tests/phpunit/tests/wp/handle404.php (12 tests): index, provider, paged, taxonomy, user and stylesheet routes with no posts, plus guards that unknown URLs and unregistered providers still 404.

  • --group sitemaps
  • --group wp --group query --group feed --group canonical --group rewrite

Screenshots

NA

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Root-cause investigation, tracing history, and drafting the fix and tests.

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props iamchitti.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant