fix(did-you-mean): suppress suggestions for valid queries and matching terms#4330
Open
faisalahammad wants to merge 2 commits into
Open
fix(did-you-mean): suppress suggestions for valid queries and matching terms#4330faisalahammad wants to merge 2 commits into
faisalahammad wants to merge 2 commits into
Conversation
…hes original term - Suppress suggestion in get_suggestion() if query found_posts > 0 - Suppress suggestion if top suggestion equals original search term (case-insensitive) - Apply same guard in automatically_redirect_user() to prevent redirect loops - Add unit tests for both new behaviors Fixes 10up#3602
burhandodhy
requested changes
Jul 6, 2026
- Add ep_suggestion_suppress_when_results_exist filter (default true) - Add test for filter opt-out behavior Addresses review feedback from burhandodhy. Refs 10up#4330
faisalahammad
commented
Jul 6, 2026
faisalahammad
left a comment
Author
There was a problem hiding this comment.
Updated per feedback. Added ep_suggestion_suppress_when_results_exist filter (default true) so sites can restore the old behavior with one line. Test added: testGetSuggestionSuppressWhenResultsExistFilter.
Tests: 21/21 TestDidYouMean pass. Full suite 944 tests, no regressions.
Lint: clean.
CodeRabbit: 0 findings.
Author
|
@burhandodhy addressed your feedback, ready for another look. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
"Did You Mean" currently shows the top suggestion even when the current search term already returns results. This leads to confusing suggestions like "Did you mean: shift?" when searching for "shirt" with valid results. This fix suppresses the suggestion when the query has results, or when the suggestion matches the original term. It also applies the same guard to the redirect behavior to prevent a redirect loop.
Closes #3602
Changes
includes/classes/Feature/DidYouMean/DidYouMean.phpget_suggestion()now returnsfalseearly in two cases:Before:
After:
Why: This matches the existing behavior of
get_alternatives_terms(), which already hides the alternative list when the query has results. The case-insensitive match guard prevents the feature from suggesting the same term the user already typed.A new
ep_suggestion_suppress_when_results_existfilter (defaulttrue) was added per review feedback, so sites that want the suggestion to still show up alongside results can opt back in with one line, instead of the fix being all-or-nothing.automatically_redirect_user()also skips redirecting if the suggested term equals the current search term:After:
Why: Prevents an infinite redirect loop when the phrase suggester returns the same term as the original query.
How to test the Change
Test 1: Valid query with results
Result: results load and no "Did you mean: shift?" message appears.
Test 2: Typo with no results
Result: "Did you mean: shirt?" appears because the query has no results.
Test 3: Same-term suggestion
Result: no suggestion is displayed.
Test 4: Restore suggestion via filter
add_filter( 'ep_suggestion_suppress_when_results_exist', '__return_false' );to a must-use plugin.Result: "Did you mean: shift?" now shows again alongside the "shirt" results.
Unit tests
Tests added/updated in
tests/php/features/TestDidYouMean.php:testGetSuggestionNotShownWhenResultsExisttestGetSuggestionNotShownWhenTopSuggestionMatchesOriginalTermtestGetSuggestionNotShownWhenTopSuggestionMatchesOriginalTermCaseInsensitivetestGetSuggestionSuppressWhenResultsExistFilterRun with:
All 21 tests pass. Full
composer run test-single-sitesuite also run (944 tests) with no regressions (1 pre-existing unrelated failure inTestInstantResultson WP 7.0, not touched by this PR).Changelog Entry
Credits
Props @faisalahammad
Checklist: