Ticket #53450: 53450_v2.diff
File 53450_v2.diff, 5.6 KB (added by , 3 years ago) |
---|
-
src/wp-includes/class-wp-meta-query.php
diff --git a/src/wp-includes/class-wp-meta-query.php b/src/wp-includes/class-wp-meta-query.php index 5a1b0c41479..b0977aa6f67 100644
a b class WP_Meta_Query { 102 102 * @since 5.1.0 Introduced $compare_key clause parameter, which enables LIKE key matches. 103 103 * @since 5.3.0 Increased the number of operators available to $compare_key. Introduced $type_key, 104 104 * which enables the $key to be cast to a new data type for comparisons. 105 * @since 5.9.0 Adds LIKE based STARTSWITH and ENDSWITH operators for value compares 105 106 * 106 107 * @param array $meta_query { 107 108 * Array of meta query clauses. When first-order clauses or sub-clauses use strings as … … class WP_Meta_Query { 123 124 * @type string $value Meta value to filter by. 124 125 * @type string $compare MySQL operator used for comparing the $value. Accepts '=', 125 126 * '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 127 * 'STARTSWITH', 'NOT STARTSWITH', 'ENDSWITH', 'NOT ENDSWITH', 126 128 * 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'REGEXP', 127 129 * 'NOT REGEXP', 'RLIKE', 'EXISTS' or 'NOT EXISTS'. 128 130 * Default is 'IN' when `$value` is an array, '=' otherwise. … … public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) 509 511 '=', 510 512 '!=', 511 513 'LIKE', 514 'STARTSWITH', 515 'ENDSWITH', 512 516 'NOT LIKE', 517 'NOT STARTSWITH', 518 'NOT ENDSWITH', 513 519 'IN', 514 520 'NOT IN', 515 521 'EXISTS', … … public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) 713 719 $where = $wpdb->prepare( '%s', $meta_value ); 714 720 break; 715 721 722 case 'STARTSWITH': 723 case 'NOT STARTSWITH': 724 $meta_compare = 'STARTSWITH' === $meta_compare ? 'LIKE' : 'NOT LIKE'; 725 $meta_value = $wpdb->esc_like( $meta_value ) . '%'; 726 $where = $wpdb->prepare( '%s', $meta_value ); 727 break; 728 729 case 'ENDSWITH': 730 case 'NOT ENDSWITH': 731 $meta_compare = 'ENDSWITH' === $meta_compare ? 'LIKE' : 'NOT LIKE'; 732 $meta_value = '%' . $wpdb->esc_like( $meta_value ); 733 $where = $wpdb->prepare( '%s', $meta_value ); 734 break; 735 716 736 // EXISTS with a value is interpreted as '='. 717 737 case 'EXISTS': 718 738 $meta_compare = '='; -
tests/phpunit/tests/query/metaQuery.php
diff --git a/tests/phpunit/tests/query/metaQuery.php b/tests/phpunit/tests/query/metaQuery.php index 5acec55d3f9..8798d9e7aff 100644
a b public function test_meta_query_single_query_compare_not_like() { 271 271 $this->assertSameSets( $expected, $query->posts ); 272 272 } 273 273 274 public function test_meta_query_single_query_compare_startswith() { 275 $p1 = self::factory()->post->create(); 276 $p2 = self::factory()->post->create(); 277 278 add_post_meta( $p1, 'foo', 'bar' ); 279 280 $query = new WP_Query( 281 array( 282 'update_post_meta_cache' => false, 283 'update_post_term_cache' => false, 284 'fields' => 'ids', 285 'meta_query' => array( 286 array( 287 'key' => 'foo', 288 'value' => 'ba', 289 'compare' => 'STARTSWITH', 290 ), 291 ), 292 ) 293 ); 294 295 $expected = array( $p1 ); 296 $this->assertSameSets( $expected, $query->posts ); 297 } 298 299 public function test_meta_query_single_query_compare_not_startswith() { 300 $p1 = self::factory()->post->create(); 301 $p2 = self::factory()->post->create(); 302 $p3 = self::factory()->post->create(); 303 304 add_post_meta( $p1, 'foo', 'bar' ); 305 add_post_meta( $p2, 'foo', 'rab' ); 306 307 $query = new WP_Query( 308 array( 309 'update_post_meta_cache' => false, 310 'update_post_term_cache' => false, 311 'fields' => 'ids', 312 'meta_query' => array( 313 array( 314 'key' => 'foo', 315 'value' => 'ba', 316 'compare' => 'NOT STARTSWITH', 317 ), 318 ), 319 ) 320 ); 321 322 $expected = array( $p2 ); 323 $this->assertSameSets( $expected, $query->posts ); 324 } 325 326 public function test_meta_query_single_query_compare_endswith() { 327 $p1 = self::factory()->post->create(); 328 $p2 = self::factory()->post->create(); 329 $p3 = self::factory()->post->create(); 330 331 add_post_meta( $p1, 'foo', 'bar' ); 332 add_post_meta( $p2, 'foo', 'foobar' ); 333 334 $query = new WP_Query( 335 array( 336 'update_post_meta_cache' => false, 337 'update_post_term_cache' => false, 338 'fields' => 'ids', 339 'meta_query' => array( 340 array( 341 'key' => 'foo', 342 'value' => 'ar', 343 'compare' => 'ENDSWITH', 344 ), 345 ), 346 ) 347 ); 348 349 $expected = array( $p1, $p2 ); 350 $this->assertSameSets( $expected, $query->posts ); 351 } 352 353 public function test_meta_query_single_query_compare_not_endswith() { 354 $p1 = self::factory()->post->create(); 355 $p2 = self::factory()->post->create(); 356 $p3 = self::factory()->post->create(); 357 358 add_post_meta( $p1, 'foo', 'bar' ); 359 add_post_meta( $p2, 'foo', 'rab' ); 360 361 $query = new WP_Query( 362 array( 363 'update_post_meta_cache' => false, 364 'update_post_term_cache' => false, 365 'fields' => 'ids', 366 'meta_query' => array( 367 array( 368 'key' => 'foo', 369 'value' => 'ar', 370 'compare' => 'NOT ENDSWITH', 371 ), 372 ), 373 ) 374 ); 375 376 $expected = array( $p2 ); 377 $this->assertSameSets( $expected, $query->posts ); 378 } 379 274 380 public function test_meta_query_single_query_compare_between_not_between() { 275 381 $p1 = self::factory()->post->create(); 276 382 $p2 = self::factory()->post->create();