Ticket #42409: 42409.3.diff
| File 42409.3.diff, 7.1 KB (added by , 8 years ago) |
|---|
-
src/wp-includes/class-wp-meta-query.php
diff --git src/wp-includes/class-wp-meta-query.php src/wp-includes/class-wp-meta-query.php index 6db272c526..61beeb3f3c 100644
class WP_Meta_Query { 99 99 * 100 100 * @since 3.2.0 101 101 * @since 4.2.0 Introduced support for naming query clauses by associative array keys. 102 * @since 5.0.0 Introduced $compare_key clause parameter, which enables LIKE key matches. 102 103 * 103 104 * @param array $meta_query { 104 105 * Array of meta query clauses. When first-order clauses or sub-clauses use strings as … … class WP_Meta_Query { 109 110 * @type array { 110 111 * Optional. An array of first-order clause parameters, or another fully-formed meta query. 111 112 * 112 * @type string $key Meta key to filter by. 113 * @type string $value Meta value to filter by. 114 * @type string $compare MySQL operator used for comparing the $value. Accepts '=', 115 * '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 116 * 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'REGEXP', 117 * 'NOT REGEXP', 'RLIKE', 'EXISTS' or 'NOT EXISTS'. 118 * Default is 'IN' when `$value` is an array, '=' otherwise. 119 * @type string $type MySQL data type that the meta_value column will be CAST to for 120 * comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 121 * 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'. 122 * Default is 'CHAR'. 113 * @type string $key Meta key to filter by. 114 * @type string $compare_key MySQL operator used for comparing the $key. Accepts '=' and 'LIKE'. 115 * @type string $value Meta value to filter by. 116 * @type string $compare MySQL operator used for comparing the $value. Accepts '=', 117 * '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 118 * 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'REGEXP', 119 * 'NOT REGEXP', 'RLIKE', 'EXISTS' or 'NOT EXISTS'. 120 * Default is 'IN' when `$value` is an array, '=' otherwise. 121 * @type string $type MySQL data type that the meta_value column will be CAST to for 122 * comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 123 * 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'. 124 * Default is 'CHAR'. 123 125 * } 124 126 * } 125 127 */ … … class WP_Meta_Query { 236 238 * the rest of the meta_query). 237 239 */ 238 240 $primary_meta_query = array(); 239 foreach ( array( 'key', 'compare', 'type' ) as $key ) {241 foreach ( array( 'key', 'compare', 'type', 'compare_key' ) as $key ) { 240 242 if ( ! empty( $qv[ "meta_$key" ] ) ) { 241 243 $primary_meta_query[ $key ] = $qv[ "meta_$key" ]; 242 244 } … … class WP_Meta_Query { 518 520 $clause['compare'] = '='; 519 521 } 520 522 523 if ( isset( $clause['compare_key'] ) && 'LIKE' === strtoupper( $clause['compare_key'] ) ) { 524 $clause['compare_key'] = strtoupper( $clause['compare_key'] ); 525 } else { 526 $clause['compare_key'] = '='; 527 } 528 521 529 $meta_compare = $clause['compare']; 530 $meta_compare_key = $clause['compare_key']; 522 531 523 532 // First build the JOIN clause, if one is required. 524 533 $join = ''; … … class WP_Meta_Query { 533 542 if ( 'NOT EXISTS' === $meta_compare ) { 534 543 $join .= " LEFT JOIN $this->meta_table"; 535 544 $join .= $i ? " AS $alias" : ''; 536 $join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] ); 545 546 if ( 'LIKE' === $meta_compare_key ) { 547 $join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key LIKE %s )", '%' . $wpdb->esc_like( $clause['key'] ) . '%' ); 548 } else { 549 $join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] ); 550 } 537 551 538 552 // All other JOIN clauses. 539 553 } else { … … class WP_Meta_Query { 577 591 if ( 'NOT EXISTS' === $meta_compare ) { 578 592 $sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL'; 579 593 } else { 580 $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) ); 594 if ( 'LIKE' === $meta_compare_key ) { 595 $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key LIKE %s", '%' . $wpdb->esc_like( trim( $clause['key'] ) ) . '%' ); 596 } else { 597 $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) ); 598 } 581 599 } 582 600 } 583 601 -
src/wp-includes/class-wp-query.php
diff --git src/wp-includes/class-wp-query.php src/wp-includes/class-wp-query.php index 930e33d883..3d92c340e2 100644
class WP_Query { 640 640 * @type int $m Combination YearMonth. Accepts any four-digit year and month 641 641 * numbers 1-12. Default empty. 642 642 * @type string $meta_compare Comparison operator to test the 'meta_value'. 643 * @type string $meta_compare_key Comparison operator to test the 'meta_key'. 643 644 * @type string $meta_key Custom field key. 644 645 * @type array $meta_query An associative array of WP_Meta_Query arguments. See WP_Meta_Query. 645 646 * @type string $meta_value Custom field value. -
tests/phpunit/tests/query/metaQuery.php
diff --git tests/phpunit/tests/query/metaQuery.php tests/phpunit/tests/query/metaQuery.php index bed26a4213..8a8e93b044 100644
class Tests_Query_MetaQuery extends WP_UnitTestCase { 1848 1848 1849 1849 $this->assertEqualSets( array( 'foo_key', 'foo_key-1', 'foo_key-2' ), array_keys( $q->meta_query->get_clauses() ) ); 1850 1850 } 1851 1852 /** 1853 * @ticket 42409 1854 */ 1855 public function test_compare_key_like() { 1856 $posts = self::factory()->post->create_many( 3 ); 1857 1858 add_post_meta( $posts[0], 'aaa_foo_aaa', 'abc' ); 1859 add_post_meta( $posts[1], 'aaa_bar_aaa', 'abc' ); 1860 add_post_meta( $posts[2], 'aaa_foo_bbb', 'abc' ); 1861 1862 $q = new WP_Query( 1863 array( 1864 'meta_query' => array( 1865 array( 1866 'compare_key' => 'LIKE', 1867 'key' => 'aa_foo', 1868 ), 1869 ), 1870 'fields' => 'ids', 1871 ) 1872 ); 1873 1874 $this->assertEqualSets( array( $posts[0], $posts[2] ), $q->posts ); 1875 } 1876 1877 /** 1878 * @ticket 42409 1879 */ 1880 public function test_meta_compare_key_like() { 1881 $posts = self::factory()->post->create_many( 3 ); 1882 1883 add_post_meta( $posts[0], 'aaa_foo_aaa', 'abc' ); 1884 add_post_meta( $posts[1], 'aaa_bar_aaa', 'abc' ); 1885 add_post_meta( $posts[2], 'aaa_foo_bbb', 'abc' ); 1886 1887 $q = new WP_Query( 1888 array( 1889 'meta_compare_key' => 'LIKE', 1890 'meta_key' => 'aa_foo', 1891 'fields' => 'ids', 1892 ) 1893 ); 1894 1895 $this->assertEqualSets( array( $posts[0], $posts[2] ), $q->posts ); 1896 } 1851 1897 }