Make WordPress Core

Opened 9 years ago

Last modified 8 years ago

#43634 new defect (bug)

WP_Query returns wrong result if 10+ search terms and all negative.

Reported by: johnh10 Owned by:
Priority: normal Milestone: Awaiting Review
Component: Query Version:
Severity: normal Keywords:
Cc: Focuses:

Description

Set up: create single post with title or body "Don't match this one!"

Use WP_query with 's' parameter:

-one -two -three -four -five -six -seven -eight -nine -ten

WP_Query will find and return the single post and it shouldn't.

Less than 10 negative search terms works fine.

I believe it has to do with a special 'if clause' in the function parse_search in class-wp-query.php

// if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 ) {
    $q['search_terms'] = array( $q['s'] );
}

this makes

JPH q['search_terms'] : Array
(
    [0] => -one -two -three -four -five -six -seven -eight -nine -ten
)

and later in the function the search variable becomes

JPH search variable is:  AND (((testsite_posts.post_title NOT LIKE '{07e030b91cb064568b0acafd83d99152667b06f2ffd415e48cd2d2f458c2c217}one -two -three -four -five -six -seven -eight -nine -ten{07e030b91cb064568b0acafd83d99152667b06f2ffd415e48cd2d2f458c2c217}') AND (testsite_posts.post_excerpt NOT LIKE '{07e030b91cb064568b0acafd83d99152667b06f2ffd415e48cd2d2f458c2c217}one -two -three -four -five -six -seven -eight -nine -ten{07e030b91cb064568b0acafd83d99152667b06f2ffd415e48cd2d2f458c2c217}') AND (testsite_posts.post_content NOT LIKE '{07e030b91cb064568b0acafd83d99152667b06f2ffd415e48cd2d2f458c2c217}one -two -three -four -five -six -seven -eight -nine -ten{07e030b91cb064568b0acafd83d99152667b06f2ffd415e48cd2d2f458c2c217}')))  AND (testsite_posts.post_password = '') 

Change History (1)

#1 @pento
8 years ago

  • Component Posts, Post Types → Query
  • Version trunk
Note: See TracTickets for help on using tickets.