#39717 closed defect (bug) (wontfix)
Search_terms_count includes stopwords
Reported by: | msaari | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Query | Keywords: | needs-unit-tests has-patch |
Focuses: | Cc: |
Description
If a search includes stopwords, the "search_terms_count" parameter will not match the number of search terms in "search_terms". If you search for "call of the wild" in an English installation, you'll get:
[search_terms_count] => 4 [search_terms] => Array ( [0] => call [1] => wild )
The correct result should be:
[search_terms_count] => 2 [search_terms] => Array ( [0] => call [1] => wild )
A simple correction would be to move the line
$q['search_terms_count'] = count( $matches[0] ); $q['search_terms'] = $this->parse_search_terms( $matches[0] );
after the search term parsing and change it to this:
$q['search_terms'] = $this->parse_search_terms( $matches[0] ); $q['search_terms_count'] = count( $q["search_terms"] );
Now the search_terms_count is correct. As far as I can tell, this has no adverse side effects.
Attachments (2)
Change History (8)
#1
follow-up:
↓ 2
@
8 years ago
- Keywords needs-patch needs-unit-tests added
- Version trunk deleted
Hey there,
Thanks for your report and welcome to WordPress Trac!
Right now your patch cannot be applied because it's not in the right format and lacks path and line number information. Would you mind creating a proper patch using for example svn diff
or git diff
. See also https://make.wordpress.org/core/handbook/tutorials/working-with-patches/ for help.
#4
@
8 years ago
- Milestone changed from Awaiting Review to 4.8
@msaari Thanks very much for the patch and for the ticket. Welcome to WordPress Trac!
It appears that 'search_terms_count' has been set in this way since its introduction in [25632], which is when search ordering was overhauled. Your fix looks correct to me.
#5
@
8 years ago
- Milestone 4.8 deleted
- Resolution set to wontfix
- Status changed from new to closed
After reviewing this a bit more, I think I disagree with my earlier self. 'search_terms_count' is used in WP to determine whether an exact-match search ordering should take place. That is: If you search for "call of the wild", you'd expect "of" and "the" to be ignored for the general search, but you'd also expect exact matches of "call of the wild" in the title or content to push the corresponding posts to the top. This exact-match ordering requires that 'search_terms_count' reflect the count of the raw terms passed to 's'
, otherwise search phrases with only a single non-stopword ("call of the") will not have exact-match ordering applied.
For this reason, I think we need to mark wontfix. If your plugin needs access to the count of non-stopwords, you can do count( $q->get( 'search_terms' ) )
.
Thanks again for the ticket.
Patch to correct /wp-includes/class-wp-query.php