-
Notifications
You must be signed in to change notification settings - Fork 3.2k
#64250 - Refactor redirect_guess_404_permalink to use WP_Query instead of raw SQL #10531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
#64250 - Refactor redirect_guess_404_permalink to use WP_Query instead of raw SQL #10531
Conversation
|
Hi @anandrajaram21! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
@westonruter I've resolved the comments. Seeking clarification on one comment and requesting for a re-review |
westonruter
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really good to me. I'll leave for @spacedmonkey to give final approval.
spacedmonkey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to tweak WP_Query a little and this should be good to go.
|
@westonruter @anandrajaram21 I have put together a little PR to use WP_Query without a filter. 4c87073. Completely untested, but this is how I think this should work. |
|
@spacedmonkey Clever. So you're leveraging the search capabilities of Full diff from base: 33c8d7e...4c87073 |
|
In looking at #10590, which has the aforementioned commit, unit tests are failing. |
|
@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! |
|
@anandrajaram21 Here is the full changeset Full diff from base: I had to make some more tweaks to WP_Query, but it seems to be working. It needs unit tests and testing. |
|
@westonruter @spacedmonkey I've made the required changes as suggested, and also tweaked the test cases to make sure they pass. Do let me know if I can go ahead and write unit tests for the modified WP_Query, or if there are any other changes to be made before I do so |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR refactors the redirect_guess_404_permalink() function in canonical.php to replace direct SQL queries with WordPress's native WP_Query abstraction layer. The changes enhance the search capabilities in WP_Query to support the post_name column and introduce a new starts_with parameter for prefix-based searches.
Changes:
- Added
post_nameas a supported search column inWP_Query::parse_search() - Introduced new
starts_withquery parameter for prefix-based searches inWP_Query - Refactored
redirect_guess_404_permalink()to useWP_Queryinstead of raw SQL queries
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/wp-includes/canonical.php | Replaced raw SQL queries with WP_Query, removed global $wpdb dependency, and implemented date queries using date_query parameter |
| src/wp-includes/class-wp-query.php | Added post_name to default search columns, introduced starts_with parameter for prefix matching, and updated documentation |
| tests/phpunit/tests/query/searchColumns.php | Updated test to use post_slug instead of post_name for testing non-supported columns |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $query_args['s'] = get_query_var( 'name' ); | ||
| $query_args['search_columns'] = array( 'post_name' ); | ||
| $query_args['starts_with'] = true; |
Copilot
AI
Jan 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new starts_with parameter usage in redirect_guess_404_permalink() lacks test coverage. Consider adding tests to verify that the 404 redirect guessing works correctly with this new parameter, particularly for the non-strict guess scenario.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@westonruter @spacedmonkey if this approach seems alright, can I go ahead and write tests, or do I have to make any more changes in the approach before I go ahead
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
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.