Changeset 42768
- Timestamp:
- 03/01/2018 04:02:41 AM (7 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-meta-query.php
r42343 r42768 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 { … … 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 * Default '='. 116 * @type string $value Meta value to filter by. 117 * @type string $compare MySQL operator used for comparing the $value. Accepts '=', 118 * '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 119 * 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'REGEXP', 120 * 'NOT REGEXP', 'RLIKE', 'EXISTS' or 'NOT EXISTS'. 121 * Default is 'IN' when `$value` is an array, '=' otherwise. 122 * @type string $type MySQL data type that the meta_value column will be CAST to for 123 * comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 124 * 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'. 125 * Default is 'CHAR'. 123 126 * } 124 127 * } … … 237 240 */ 238 241 $primary_meta_query = array(); 239 foreach ( array( 'key', 'compare', 'type' ) as $key ) {242 foreach ( array( 'key', 'compare', 'type', 'compare_key' ) as $key ) { 240 243 if ( ! empty( $qv[ "meta_$key" ] ) ) { 241 244 $primary_meta_query[ $key ] = $qv[ "meta_$key" ]; … … 519 522 } 520 523 521 $meta_compare = $clause['compare']; 524 if ( isset( $clause['compare_key'] ) && 'LIKE' === strtoupper( $clause['compare_key'] ) ) { 525 $clause['compare_key'] = strtoupper( $clause['compare_key'] ); 526 } else { 527 $clause['compare_key'] = '='; 528 } 529 530 $meta_compare = $clause['compare']; 531 $meta_compare_key = $clause['compare_key']; 522 532 523 533 // First build the JOIN clause, if one is required. … … 534 544 $join .= " LEFT JOIN $this->meta_table"; 535 545 $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'] ); 546 547 if ( 'LIKE' === $meta_compare_key ) { 548 $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'] ) . '%' ); 549 } else { 550 $join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] ); 551 } 537 552 538 553 // All other JOIN clauses. … … 578 593 $sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL'; 579 594 } else { 580 $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) ); 595 if ( 'LIKE' === $meta_compare_key ) { 596 $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key LIKE %s", '%' . $wpdb->esc_like( trim( $clause['key'] ) ) . '%' ); 597 } else { 598 $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) ); 599 } 581 600 } 582 601 } -
trunk/src/wp-includes/class-wp-query.php
r42581 r42768 605 605 * @since 4.6.0 Added 'post_name__in' support for `$orderby`. Introduced the `$lazy_load_term_meta` argument. 606 606 * @since 4.9.0 Introduced the `$comment_count` parameter. 607 * @since 5.0.0 Introduced the `$meta_compare_key` parameter. 607 608 * 608 609 * @param string|array $query { … … 641 642 * numbers 1-12. Default empty. 642 643 * @type string $meta_compare Comparison operator to test the 'meta_value'. 644 * @type string $meta_compare_key Comparison operator to test the 'meta_key'. 643 645 * @type string $meta_key Custom field key. 644 646 * @type array $meta_query An associative array of WP_Meta_Query arguments. See WP_Meta_Query. -
trunk/tests/phpunit/tests/query/metaQuery.php
r42343 r42768 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 } 1897 1898 /** 1899 * @ticket 42409 1900 */ 1901 public function test_compare_key_like_with_not_exists_compare() { 1902 $posts = self::factory()->post->create_many( 3 ); 1903 1904 add_post_meta( $posts[0], 'aaa_foo_aaa', 'abc' ); 1905 add_post_meta( $posts[1], 'aaa_bar_aaa', 'abc' ); 1906 add_post_meta( $posts[2], 'bar', 'abc' ); 1907 1908 $q = new WP_Query( 1909 array( 1910 'meta_query' => array( 1911 'relation' => 'AND', 1912 array( 1913 'compare_key' => 'LIKE', 1914 'key' => 'bar', 1915 'compare' => 'NOT EXISTS', 1916 ), 1917 ), 1918 'fields' => 'ids', 1919 ) 1920 ); 1921 1922 $this->assertEqualSets( array( $posts[0] ), $q->posts ); 1923 1924 } 1851 1925 }
Note: See TracChangeset
for help on using the changeset viewer.