Skip to content

Conversation

@anandrajaram21
Copy link

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.

@github-actions
Copy link

Hi @anandrajaram21! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

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 WordPress Project

@github-actions
Copy link

github-actions bot commented Nov 19, 2025

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 altf4falt, westonruter, spacedmonkey.

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

@github-actions
Copy link

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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • 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.

@anandrajaram21 anandrajaram21 changed the title refactor: replace direct SQL with WP_Query in redirect_guess_404_perm… #64250 - Refactor redirect_guess_404_permalink to use WP_Query instead of raw SQL Nov 19, 2025
@anandrajaram21
Copy link
Author

@westonruter I've resolved the comments. Seeking clarification on one comment and requesting for a re-review

Copy link
Member

@westonruter westonruter left a 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.

Copy link
Member

@spacedmonkey spacedmonkey left a 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.

@spacedmonkey
Copy link
Member

@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.

@westonruter
Copy link
Member

westonruter commented Dec 3, 2025

@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: 33c8d7e...4c87073

@westonruter
Copy link
Member

westonruter commented Dec 3, 2025

In looking at #10590, which has the aforementioned commit, unit tests are failing.

@anandrajaram21
Copy link
Author

@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
Copy link
Member

@anandrajaram21 Here is the full changeset Full diff from base:
33c8d7e...ff81f88

I had to make some more tweaks to WP_Query, but it seems to be working. It needs unit tests and testing.

@anandrajaram21
Copy link
Author

@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

Copy link

Copilot AI left a 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_name as a supported search column in WP_Query::parse_search()
  • Introduced new starts_with query parameter for prefix-based searches in WP_Query
  • Refactored redirect_guess_404_permalink() to use WP_Query instead 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.

Comment on lines +986 to +988
$query_args['s'] = get_query_var( 'name' );
$query_args['search_columns'] = array( 'post_name' );
$query_args['starts_with'] = true;
Copy link

Copilot AI Jan 14, 2026

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.

Copilot uses AI. Check for mistakes.
Copy link
Author

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

Copy link
Member

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];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can now use:

Suggested change
$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];

Copy link
Member

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:

Suggested change
if ( ! is_int( $post_id ) ) {
return false;
}

Copy link

Copilot AI left a 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' );
Copy link

Copilot AI Jan 17, 2026

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.

Copilot uses AI. Check for mistakes.
* @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.
Copy link

Copilot AI Jan 17, 2026

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.

Copilot uses AI. Check for mistakes.
Copy link
Member

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.

Comment on lines +690 to +692
* 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`.
Copy link

Copilot AI Jan 17, 2026

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.

Copilot uses AI. Check for mistakes.
Copy link
Member

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.

Comment on lines +986 to +988
$query_args['s'] = get_query_var( 'name' );
$query_args['search_columns'] = array( 'post_name' );
$query_args['starts_with'] = true;
Copy link

Copilot AI Jan 17, 2026

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.

Copilot uses AI. Check for mistakes.
Comment on lines +986 to +988
$query_args['s'] = get_query_var( 'name' );
$query_args['search_columns'] = array( 'post_name' );
$query_args['starts_with'] = true;
Copy link

Copilot AI Jan 17, 2026

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.

Copilot uses AI. Check for mistakes.
$query_args['post_type'] = $publicly_viewable_post_types;
}

// Handle date queries.
Copy link

Copilot AI Jan 17, 2026

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.

Suggested change
// Handle date queries.
// Build date_query array from individual year, month, and day query vars for WP_Query compatibility.

Copilot uses AI. Check for mistakes.
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'fields' => 'ids',
'orderby' => 'none',
Copy link
Member

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.

Suggested change
'orderby' => 'none',
'orderby' => 'none',
'suppress_filters' => true,

Copy link

Copilot AI left a 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.

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.

3 participants