Opened 4 weeks ago
Last modified 4 days ago
#64250 reviewing enhancement
Refactor redirect_guess_404_permalink to use WP_Query instead of raw SQL
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 7.0 | Priority: | normal |
| Severity: | normal | Version: | 2.3 |
| Component: | Permalinks | Keywords: | has-patch |
| Focuses: | performance | Cc: |
Description
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.
Change History (10)
This ticket was mentioned in PR #10531 on WordPress/wordpress-develop by @altf4falt.
3 weeks ago
#1
- Keywords has-patch added
#2
@
3 weeks ago
I've refactored the function to use WP Query instead of direct SQL queries.
PR Link: https://github.com/WordPress/wordpress-develop/pull/10531
@altf4falt commented on PR #10531:
3 weeks ago
#3
@westonruter I've resolved the comments. Seeking clarification on one comment and requesting for a re-review
#4
@
2 weeks ago
- Keywords good-first-bug removed
- Milestone changed from Future Release to 7.0
- Owner set to spacedmonkey
- Status changed from new to reviewing
This ticket was mentioned in PR #10590 on WordPress/wordpress-develop by @spacedmonkey.
7 days ago
#5
Trac ticket:
@spacedmonkey commented on PR #10531:
7 days ago
#6
@westonruter @anandrajaram21 I have put together a little PR to use WP_Query without a filter. https://github.com/WordPress/wordpress-develop/pull/10590/commits/4c87073adeed8643d01d8a5934d7c8c5c00cf889. Completely untested, but this is how I think this should work.
@westonruter commented on PR #10531:
6 days ago
#7
@spacedmonkey Clever. So you're leveraging the search capabilities of WP_Query to implement fuzzy redirect guessing, specifically via restricting the search_columns to just include the post_name.
Full diff from base: https://github.com/WordPress/wordpress-develop/compare/33c8d7efa3d96ddd9a87168dfe15e771fb81d29c...4c87073adeed8643d01d8a5934d7c8c5c00cf889
@westonruter commented on PR #10531:
6 days ago
#8
In looking at https://github.com/WordPress/wordpress-develop/pull/10590 which has the aforementioned commit, unit tests are failing.
@altf4falt commented on PR #10531:
5 days ago
#9
@spacedmonkey This approach makes sense. I'll try this out, however as @westonruter mentioned, the tests do not pass, so I'll try making the tests pass while keeping the core idea in mind. Thanks for the input!
@spacedmonkey commented on PR #10531:
4 days ago
#10
@anandrajaram21 Here is the full changeset Full diff from base:
https://github.com/WordPress/wordpress-develop/compare/33c8d7efa3d96ddd9a87168dfe15e771fb81d29c...ff81f8864ce2277b9c2c3596315af4e55942960b
I had to make some more tweaks to WP_Query, but it seems to be working. It needs unit tests and testing.
The redirect_guess_404_permalink() function in src/wp-includes/canonical.php has been refactored to replace direct SQL queries with WordPress's native WP_Query abstraction layer.
Trac ticket: https://core.trac.wordpress.org/ticket/64250