[wp-trac] [WordPress Trac] #64250: Refactor redirect_guess_404_permalink to use WP_Query instead of raw SQL
WordPress Trac
noreply at wordpress.org
Thu Nov 13 16:22:45 UTC 2025
#64250: Refactor redirect_guess_404_permalink to use WP_Query instead of raw SQL
--------------------------+----------------------------
Reporter: spacedmonkey | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Future Release
Component: Permalinks | Version: 2.3
Severity: normal | Keywords: good-first-bug
Focuses: performance |
--------------------------+----------------------------
The current implementation of the redirect_guess_404_permalink() function
uses a direct SQL query to retrieve post IDs when attempting to guess the
correct URL for a 404 request based on query variables. While this
approach works, it bypasses WordPress’s built-in query abstraction layer
and associated benefits.
This change proposes replacing the raw SQL with a WP_Query implementation.
Doing so will improve maintainability, consistency, and performance
through native WordPress features such as:
• Query caching – Utilises WordPress’s object caching layer,
reducing redundant database queries.
• Filtering and extensibility – Enables the use of hooks and
filters that developers may rely on to modify query behaviour.
• Security and readability – Removes the need for manual SQL
sanitisation and improves code clarity.
• Future compatibility – Ensures the function remains
aligned with core WordPress query handling and schema changes.
The refactor should maintain the same functional behaviour — guessing the
most appropriate redirect target for a 404 request — while ensuring no
regression in performance or accuracy.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/64250>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list