-
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
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'll defer to @spacedmonkey since this was his suggestion. But it's looking good for far to me.
| return false; | ||
| } | ||
|
|
||
| $post_id = $query->posts[0]; |
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 can now use:
| $post_id = $query->posts[0]; | |
| $post_id = array_first( $query->posts ); |
This is polyfilled in core as of WP 6.9. The function was introduced in PHP 8.5. This is better since a posts_results filter could try doing array_filter() and the result could be array keys that don't have index 0.
| } | ||
|
|
||
| $post_id = $query->posts[0]; | ||
|
|
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.
As with the above, to be extra safe:
| if ( ! is_int( $post_id ) ) { | |
| return false; | |
| } | |
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
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $query_vars['search_orderby_title'] = array(); | ||
|
|
||
| $default_search_columns = array( 'post_title', 'post_excerpt', 'post_content' ); | ||
| $default_search_columns = array( 'post_title', 'post_excerpt', 'post_content', 'post_name' ); |
Copilot
AI
Jan 17, 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.
Adding post_name to the default search columns changes the behavior of all existing searches that don't explicitly specify search_columns. This means that searches will now match against post slugs by default, which could return unexpected results for users. For example, a search for "hello" would now match a post with slug "hello-world" even if the post title, excerpt, or content don't contain "hello". This is a significant behavioral change that could affect backwards compatibility. Consider documenting this breaking change or making this opt-in rather than default behavior.
| * @since 5.3.0 Introduced the `$meta_type_key` parameter. | ||
| * @since 6.1.0 Introduced the `$update_menu_item_cache` parameter. | ||
| * @since 6.2.0 Introduced the `$search_columns` parameter. | ||
| * @since 7.0.0 Introduced the `$starts_with` parameter. |
Copilot
AI
Jan 17, 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 test file for public query variables (tests/phpunit/tests/query/vars.php) needs to be updated to include starts_with in the expected list of public query vars, similar to how exact is listed. Without this update, the test will fail once starts_with is added to the public query vars.
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.
To do once implementation is finalized.
| * Cannot be used together with `$starts_with`. | ||
| * @type bool $starts_with Whether to search starts with keyword. Default false. | ||
| * Cannot be used together with `$exact`. |
Copilot
AI
Jan 17, 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 query parameter lacks test coverage. Tests should be added to verify: 1) that starts_with correctly searches for terms at the beginning of columns, 2) that it cannot be used together with exact (triggering the mutual exclusivity check), and 3) that it works correctly with different search columns including post_name.
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.
To do once implementation is finalized.
| $query_args['s'] = get_query_var( 'name' ); | ||
| $query_args['search_columns'] = array( 'post_name' ); | ||
| $query_args['starts_with'] = true; |
Copilot
AI
Jan 17, 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 refactored code uses WP_Query's search functionality with search_columns set to post_name only, but this relies on the new behavior where post_name is included in the default search columns. If the previous comment about adding post_name to default search columns is addressed by making it opt-in, this code will need to be updated to explicitly ensure post_name searching is supported regardless of the default behavior.
| $query_args['s'] = get_query_var( 'name' ); | ||
| $query_args['search_columns'] = array( 'post_name' ); | ||
| $query_args['starts_with'] = true; |
Copilot
AI
Jan 17, 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 refactored non-strict guess implementation introduces a behavioral change regarding password-protected posts. When using WP_Query with search functionality, the parse_search method automatically excludes password-protected posts for non-logged-in users (see line 1546). The original SQL implementation did not have this restriction and would redirect to password-protected posts. This could break existing functionality where 404 redirects would work for password-protected posts.
| $query_args['post_type'] = $publicly_viewable_post_types; | ||
| } | ||
|
|
||
| // Handle date queries. |
Copilot
AI
Jan 17, 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 comment says "Handle date queries" but this should be more descriptive about what's happening. Consider updating to "Build date_query array from individual year, month, and day query vars for WP_Query compatibility" to better explain the transformation being performed.
| // Handle date queries. | |
| // Build date_query array from individual year, month, and day query vars for WP_Query compatibility. |
| 'update_post_meta_cache' => false, | ||
| 'update_post_term_cache' => false, | ||
| 'fields' => 'ids', | ||
| 'orderby' => 'none', |
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.
@spacedmonkey Should this also include suppress_filters`? It wasn't filterable before, so this would prevent accidental filtering.
| 'orderby' => 'none', | |
| 'orderby' => 'none', | |
| 'suppress_filters' => true, |
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.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
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.