[wp-trac] [WordPress Trac] #35361: Error in SQL syntax search page

WordPress Trac noreply at wordpress.org
Fri Jan 8 16:05:13 UTC 2016


#35361: Error in SQL syntax search page
----------------------------+------------------------------
 Reporter:  salvoaranzulla  |       Owner:
     Type:  defect (bug)    |      Status:  new
 Priority:  normal          |   Milestone:  Awaiting Review
Component:  Query           |     Version:  4.4.1
 Severity:  normal          |  Resolution:
 Keywords:  dev-feedback    |     Focuses:
----------------------------+------------------------------

Comment (by salvoaranzulla):

 The problems is in query.php:

 if ( $q['search_terms_count'] > 1 ) {
             $num_terms = count( $q['search_orderby_title'] );

             // If the search terms contain negative queries, don't bother
 ordering by sentence matches.
             $like = '';
             if ( ! preg_match( '/(?:\s|^)\-/', $q['s'] ) ) {
                 $like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
             }

             $search_orderby = '(CASE ';

             // sentence match in 'post_title'
             if ( $like ) {
                 $search_orderby .= $wpdb->prepare( "WHEN
 $wpdb->posts.post_title LIKE %s THEN 1 ", $like );
             }

             // sanity limit, sort as sentence when more than 6 terms
             // (few searches are longer than 6 terms and most titles are
 not)
             if ( $num_terms < 7 ) {
                 // all words in title
                 $search_orderby .= 'WHEN ' . implode( ' AND ',
 $q['search_orderby_title'] ) . ' THEN 2 ';
                 // any word in title, not needed when $num_terms == 1
                 if ( $num_terms > 1 )
                     $search_orderby .= 'WHEN ' . implode( ' OR ',
 $q['search_orderby_title'] ) . ' THEN 3 ';
             }

             // sentence match in 'post_content'
             if ( $like ) {
                 $search_orderby .= $wpdb->prepare( "WHEN
 $wpdb->posts.post_content LIKE %s THEN 4 ", $like );
             }
             $search_orderby .= 'ELSE 5 END)';
         } else {
             // single word or sentence search
             $search_orderby = reset( $q['search_orderby_title'] ) . '
 DESC';
         }


 If $like is empty Wordpress generates an error because $num_terms >= 7.

 You can reproduce this error searching:

 LENOVO Lenovo TAB 2 A10 -70L ZA010058DE blu

 A solution could be :

 Old: if ( $num_terms < 7 ) {
 New: if ( empty( $like ) || $num_terms < 7 ) {

 Thanks

--
Ticket URL: <https://core.trac.wordpress.org/ticket/35361#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list