WordPress.org

Make WordPress Core

Opened 17 months ago

Closed 17 months ago

Last modified 17 months ago

#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)

wp-query-patch.patch (137 bytes) - added by msaari 17 months ago.
Patch to correct /wp-includes/class-wp-query.php
search_terms_count.patch (799 bytes) - added by msaari 17 months ago.
Patch to correct /wp-includes/class-wp-query.php, in correct format

Download all attachments as: .zip

Change History (8)

@msaari
17 months ago

Patch to correct /wp-includes/class-wp-query.php

#1 follow-up: @swissspidy
17 months 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.

@msaari
17 months ago

Patch to correct /wp-includes/class-wp-query.php, in correct format

#2 in reply to: ↑ 1 @msaari
17 months ago

Thanks, I've now attached a patch in correct format.

#3 @Presskopp
17 months ago

  • Keywords has-patch added; needs-patch removed

#4 @boonebgorges
17 months 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 @boonebgorges
17 months 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.

#6 @msaari
17 months ago

Thanks for looking into this.

Note: See TracTickets for help on using tickets.