Changeset 42768 for trunk/src/wp-includes/class-wp-meta-query.php
- Timestamp:
- 03/01/2018 04:02:41 AM (8 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/class-wp-meta-query.php (modified) (6 diffs)
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 }
Note: See TracChangeset
for help on using the changeset viewer.