Allow `LIKE` queries against the 'key' value in meta queries.
The new `compare_key=LIKE` parameter works in conjunction with `key` in a similar way to the `compare=LIKE` and `value`: by doing a "compares" `LIKE` query. This allows developers to do partial matches against keys when doing meta queries. Props mariovalney, chasewg. Fixes #42409. Built from https://develop.svn.wordpress.org/trunk@42768 git-svn-id: http://core.svn.wordpress.org/trunk@42598 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
d2d64f13c7
commit
35ad9e9efc
|
@ -99,6 +99,7 @@ class WP_Meta_Query {
|
||||||
*
|
*
|
||||||
* @since 3.2.0
|
* @since 3.2.0
|
||||||
* @since 4.2.0 Introduced support for naming query clauses by associative array keys.
|
* @since 4.2.0 Introduced support for naming query clauses by associative array keys.
|
||||||
|
* @since 5.0.0 Introduced $compare_key clause parameter, which enables LIKE key matches.
|
||||||
*
|
*
|
||||||
* @param array $meta_query {
|
* @param array $meta_query {
|
||||||
* Array of meta query clauses. When first-order clauses or sub-clauses use strings as
|
* Array of meta query clauses. When first-order clauses or sub-clauses use strings as
|
||||||
|
@ -109,17 +110,19 @@ class WP_Meta_Query {
|
||||||
* @type array {
|
* @type array {
|
||||||
* Optional. An array of first-order clause parameters, or another fully-formed meta query.
|
* Optional. An array of first-order clause parameters, or another fully-formed meta query.
|
||||||
*
|
*
|
||||||
* @type string $key Meta key to filter by.
|
* @type string $key Meta key to filter by.
|
||||||
* @type string $value Meta value to filter by.
|
* @type string $compare_key MySQL operator used for comparing the $key. Accepts '=' and 'LIKE'.
|
||||||
* @type string $compare MySQL operator used for comparing the $value. Accepts '=',
|
* Default '='.
|
||||||
* '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE',
|
* @type string $value Meta value to filter by.
|
||||||
* 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'REGEXP',
|
* @type string $compare MySQL operator used for comparing the $value. Accepts '=',
|
||||||
* 'NOT REGEXP', 'RLIKE', 'EXISTS' or 'NOT EXISTS'.
|
* '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE',
|
||||||
* Default is 'IN' when `$value` is an array, '=' otherwise.
|
* 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'REGEXP',
|
||||||
* @type string $type MySQL data type that the meta_value column will be CAST to for
|
* 'NOT REGEXP', 'RLIKE', 'EXISTS' or 'NOT EXISTS'.
|
||||||
* comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE',
|
* Default is 'IN' when `$value` is an array, '=' otherwise.
|
||||||
* 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'.
|
* @type string $type MySQL data type that the meta_value column will be CAST to for
|
||||||
* Default is 'CHAR'.
|
* comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE',
|
||||||
|
* 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'.
|
||||||
|
* Default is 'CHAR'.
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
|
@ -236,7 +239,7 @@ class WP_Meta_Query {
|
||||||
* the rest of the meta_query).
|
* the rest of the meta_query).
|
||||||
*/
|
*/
|
||||||
$primary_meta_query = array();
|
$primary_meta_query = array();
|
||||||
foreach ( array( 'key', 'compare', 'type' ) as $key ) {
|
foreach ( array( 'key', 'compare', 'type', 'compare_key' ) as $key ) {
|
||||||
if ( ! empty( $qv[ "meta_$key" ] ) ) {
|
if ( ! empty( $qv[ "meta_$key" ] ) ) {
|
||||||
$primary_meta_query[ $key ] = $qv[ "meta_$key" ];
|
$primary_meta_query[ $key ] = $qv[ "meta_$key" ];
|
||||||
}
|
}
|
||||||
|
@ -518,7 +521,14 @@ class WP_Meta_Query {
|
||||||
$clause['compare'] = '=';
|
$clause['compare'] = '=';
|
||||||
}
|
}
|
||||||
|
|
||||||
$meta_compare = $clause['compare'];
|
if ( isset( $clause['compare_key'] ) && 'LIKE' === strtoupper( $clause['compare_key'] ) ) {
|
||||||
|
$clause['compare_key'] = strtoupper( $clause['compare_key'] );
|
||||||
|
} else {
|
||||||
|
$clause['compare_key'] = '=';
|
||||||
|
}
|
||||||
|
|
||||||
|
$meta_compare = $clause['compare'];
|
||||||
|
$meta_compare_key = $clause['compare_key'];
|
||||||
|
|
||||||
// First build the JOIN clause, if one is required.
|
// First build the JOIN clause, if one is required.
|
||||||
$join = '';
|
$join = '';
|
||||||
|
@ -533,7 +543,12 @@ class WP_Meta_Query {
|
||||||
if ( 'NOT EXISTS' === $meta_compare ) {
|
if ( 'NOT EXISTS' === $meta_compare ) {
|
||||||
$join .= " LEFT JOIN $this->meta_table";
|
$join .= " LEFT JOIN $this->meta_table";
|
||||||
$join .= $i ? " AS $alias" : '';
|
$join .= $i ? " AS $alias" : '';
|
||||||
$join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
|
|
||||||
|
if ( 'LIKE' === $meta_compare_key ) {
|
||||||
|
$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'] ) . '%' );
|
||||||
|
} else {
|
||||||
|
$join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
|
||||||
|
}
|
||||||
|
|
||||||
// All other JOIN clauses.
|
// All other JOIN clauses.
|
||||||
} else {
|
} else {
|
||||||
|
@ -577,7 +592,11 @@ class WP_Meta_Query {
|
||||||
if ( 'NOT EXISTS' === $meta_compare ) {
|
if ( 'NOT EXISTS' === $meta_compare ) {
|
||||||
$sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';
|
$sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';
|
||||||
} else {
|
} else {
|
||||||
$sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
|
if ( 'LIKE' === $meta_compare_key ) {
|
||||||
|
$sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key LIKE %s", '%' . $wpdb->esc_like( trim( $clause['key'] ) ) . '%' );
|
||||||
|
} else {
|
||||||
|
$sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -604,6 +604,7 @@ class WP_Query {
|
||||||
* Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts.
|
* Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts.
|
||||||
* @since 4.6.0 Added 'post_name__in' support for `$orderby`. Introduced the `$lazy_load_term_meta` argument.
|
* @since 4.6.0 Added 'post_name__in' support for `$orderby`. Introduced the `$lazy_load_term_meta` argument.
|
||||||
* @since 4.9.0 Introduced the `$comment_count` parameter.
|
* @since 4.9.0 Introduced the `$comment_count` parameter.
|
||||||
|
* @since 5.0.0 Introduced the `$meta_compare_key` parameter.
|
||||||
*
|
*
|
||||||
* @param string|array $query {
|
* @param string|array $query {
|
||||||
* Optional. Array or string of Query parameters.
|
* Optional. Array or string of Query parameters.
|
||||||
|
@ -640,6 +641,7 @@ class WP_Query {
|
||||||
* @type int $m Combination YearMonth. Accepts any four-digit year and month
|
* @type int $m Combination YearMonth. Accepts any four-digit year and month
|
||||||
* numbers 1-12. Default empty.
|
* numbers 1-12. Default empty.
|
||||||
* @type string $meta_compare Comparison operator to test the 'meta_value'.
|
* @type string $meta_compare Comparison operator to test the 'meta_value'.
|
||||||
|
* @type string $meta_compare_key Comparison operator to test the 'meta_key'.
|
||||||
* @type string $meta_key Custom field key.
|
* @type string $meta_key Custom field key.
|
||||||
* @type array $meta_query An associative array of WP_Meta_Query arguments. See WP_Meta_Query.
|
* @type array $meta_query An associative array of WP_Meta_Query arguments. See WP_Meta_Query.
|
||||||
* @type string $meta_value Custom field value.
|
* @type string $meta_value Custom field value.
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '5.0-alpha-42767';
|
$wp_version = '5.0-alpha-42768';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue