Make WordPress Core


Ignore:
Timestamp:
03/01/2018 04:02:41 AM (8 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-meta-query.php

    r42343 r42768  
    100100     * @since 3.2.0
    101101     * @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.
    102103     *
    103104     * @param array $meta_query {
     
    110111     *         Optional. An array of first-order clause parameters, or another fully-formed meta query.
    111112     *
    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'.
    123126     *     }
    124127     * }
     
    237240         */
    238241        $primary_meta_query = array();
    239         foreach ( array( 'key', 'compare', 'type' ) as $key ) {
     242        foreach ( array( 'key', 'compare', 'type', 'compare_key' ) as $key ) {
    240243            if ( ! empty( $qv[ "meta_$key" ] ) ) {
    241244                $primary_meta_query[ $key ] = $qv[ "meta_$key" ];
     
    519522        }
    520523
    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'];
    522532
    523533        // First build the JOIN clause, if one is required.
     
    534544                $join .= " LEFT JOIN $this->meta_table";
    535545                $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                }
    537552
    538553                // All other JOIN clauses.
     
    578593                $sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';
    579594            } 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                }
    581600            }
    582601        }
Note: See TracChangeset for help on using the changeset viewer.