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.

Comment on lines +989 to +1004
// For loose matching (LIKE), we'll use a posts_where filter.
$post_name_for_filter = get_query_var( 'name' );

// Store the filter callback so we can remove it later.
$post_name_where_filter = static function ( $where, $query ) use ( $post_name_for_filter, $wpdb ) {
// Only apply to our specific query.
if ( isset( $query->query_vars['redirect_guess_404'] ) ) {
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_name LIKE %s", $wpdb->esc_like( $post_name_for_filter ) . '%' );
}
return $where;
};

add_filter( 'posts_where', $post_name_where_filter, 10, 2 );

// Mark this query so our filter knows to apply the LIKE clause.
$query_args['redirect_guess_404'] = true;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// For loose matching (LIKE), we'll use a posts_where filter.
$post_name_for_filter = get_query_var( 'name' );
// Store the filter callback so we can remove it later.
$post_name_where_filter = static function ( $where, $query ) use ( $post_name_for_filter, $wpdb ) {
// Only apply to our specific query.
if ( isset( $query->query_vars['redirect_guess_404'] ) ) {
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_name LIKE %s", $wpdb->esc_like( $post_name_for_filter ) . '%' );
}
return $where;
};
add_filter( 'posts_where', $post_name_where_filter, 10, 2 );
// Mark this query so our filter knows to apply the LIKE clause.
$query_args['redirect_guess_404'] = true;
$query_args['s'] = get_query_var( 'name' );
$query_args['search_columns'] = array( 'post_name' );

Currently, WP_Query does not support search column of post name, but it is a very small tweak to make this happen.

Comment on lines +1042 to +1045
// Clean up the filter if we added it (remove only our specific callback).
if ( ! $strict_guess && isset( $post_name_where_filter ) ) {
remove_filter( 'posts_where', $post_name_where_filter, 10 );
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Clean up the filter if we added it (remove only our specific callback).
if ( ! $strict_guess && isset( $post_name_where_filter ) ) {
remove_filter( 'posts_where', $post_name_where_filter, 10 );
}

Should not be needed once WP_Query is tweaked.

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

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