Ticket #33988: 33988.diff
| File 33988.diff, 3.7 KB (added by , 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 { 2127 2127 $searchand = ''; 2128 2128 $q['search_orderby_title'] = array(); 2129 2129 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 ) { 2131 2142 $like = '%' . $wpdb->esc_like( $term ) . '%'; 2132 2143 $q['search_orderby_title'][] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $like ); 2133 2144 } 2134 2145 2135 2146 $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 ); 2137 2148 $searchand = ' AND '; 2138 2149 } 2139 2150 … … class WP_Query { 2233 2244 2234 2245 if ( $q['search_terms_count'] > 1 ) { 2235 2246 $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 } 2237 2253 2238 2254 $search_orderby = '(CASE '; 2255 2239 2256 // 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 } 2241 2260 2242 2261 // sanity limit, sort as sentence when more than 6 terms 2243 2262 // (few searches are longer than 6 terms and most titles are not) … … class WP_Query { 2250 2269 } 2251 2270 2252 2271 // 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 } 2254 2275 $search_orderby .= 'ELSE 5 END)'; 2255 2276 } else { 2256 2277 // 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 { 58 58 function filter_wp_search_stopwords() { 59 59 return array(); 60 60 } 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 } 61 103 }
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)