Make WordPress Core

Ticket #33988: 33988.diff

File 33988.diff, 3.7 KB (added by boonebgorges, 11 years ago)
  • src/wp-includes/query.php

    diff --git src/wp-includes/query.php src/wp-includes/query.php
    index b93d027..5e1e6e8 100644
    class WP_Query {  
    21272127                $searchand = '';
    21282128                $q['search_orderby_title'] = array();
    21292129                foreach ( $q['search_terms'] as $term ) {
    2130                         if ( $n ) {
     2130                        // Terms prefixed with '-' should be excluded.
     2131                        $include = '-' !== substr( $term, 0, 1 );
     2132                        if ( $include ) {
     2133                                $like_op  = 'LIKE';
     2134                                $andor_op = 'OR';
     2135                        } else {
     2136                                $like_op  = 'NOT LIKE';
     2137                                $andor_op = 'AND';
     2138                                $term     = substr( $term, 1 );
     2139                        }
     2140
     2141                        if ( $n && $include ) {
    21312142                                $like = '%' . $wpdb->esc_like( $term ) . '%';
    21322143                                $q['search_orderby_title'][] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $like );
    21332144                        }
    21342145
    21352146                        $like = $n . $wpdb->esc_like( $term ) . $n;
    2136                         $search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title LIKE %s) OR ($wpdb->posts.post_content LIKE %s))", $like, $like );
     2147                        $search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s))", $like, $like );
    21372148                        $searchand = ' AND ';
    21382149                }
    21392150
    … … class WP_Query {  
    22332244
    22342245                if ( $q['search_terms_count'] > 1 ) {
    22352246                        $num_terms = count( $q['search_orderby_title'] );
    2236                         $like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
     2247
     2248                        // If the search terms contain negative queries, don't bother ordering by sentence matches.
     2249                        $like = '';
     2250                        if ( ! preg_match( '/(?:\s|^)\-/', $q['s'] ) ) {
     2251                                $like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
     2252                        }
    22372253
    22382254                        $search_orderby = '(CASE ';
     2255
    22392256                        // sentence match in 'post_title'
    2240                         $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_title LIKE %s THEN 1 ", $like );
     2257                        if ( $like ) {
     2258                                $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_title LIKE %s THEN 1 ", $like );
     2259                        }
    22412260
    22422261                        // sanity limit, sort as sentence when more than 6 terms
    22432262                        // (few searches are longer than 6 terms and most titles are not)
    … … class WP_Query {  
    22502269                        }
    22512270
    22522271                        // sentence match in 'post_content'
    2253                         $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_content LIKE %s THEN 4 ", $like );
     2272                        if ( $like ) {
     2273                                $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_content LIKE %s THEN 4 ", $like );
     2274                        }
    22542275                        $search_orderby .= 'ELSE 5 END)';
    22552276                } else {
    22562277                        // single word or sentence search
  • tests/phpunit/tests/query/search.php

    diff --git tests/phpunit/tests/query/search.php tests/phpunit/tests/query/search.php
    index 9ef9126..78bea0f 100644
    class Tests_Query_Search extends WP_UnitTestCase {  
    5858        function filter_wp_search_stopwords() {
    5959                return array();
    6060        }
     61
     62        /**
     63         * @ticket 33988
     64         */
     65        public function test_s_should_exclude_term_prefixed_with_dash() {
     66                $p1 = $this->factory->post->create( array(
     67                        'post_status' => 'publish',
     68                        'post_content' => 'This post has foo but also bar',
     69                ) );
     70                $p2 = $this->factory->post->create( array(
     71                        'post_status' => 'publish',
     72                        'post_content' => 'This post has only foo',
     73                ) );
     74
     75                $q = new WP_Query( array(
     76                        's' => 'foo -bar',
     77                        'fields' => 'ids',
     78                ) );
     79
     80                $this->assertEqualSets( array( $p2 ), $q->posts );
     81        }
     82
     83        /**
     84         * @ticket 33988
     85         */
     86        public function test_s_should_exclude_first_term_if_prefixed_with_dash() {
     87                $p1 = $this->factory->post->create( array(
     88                        'post_status' => 'publish',
     89                        'post_content' => 'This post has foo but also bar',
     90                ) );
     91                $p2 = $this->factory->post->create( array(
     92                        'post_status' => 'publish',
     93                        'post_content' => 'This post has only bar',
     94                ) );
     95
     96                $q = new WP_Query( array(
     97                        's' => '-foo bar',
     98                        'fields' => 'ids',
     99                ) );
     100
     101                $this->assertEqualSets( array( $p2 ), $q->posts );
     102        }
    61103}