Skip to content

Commit 391285a

Browse files
Meta: Add like_escape support to WP_Meta_Query
1 parent c9bdbe0 commit 391285a

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

src/wp-includes/class-wp-meta-query.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public function parse_query_vars( $qv ) {
278278
* the rest of the meta_query).
279279
*/
280280
$primary_meta_query = array();
281-
foreach ( array( 'key', 'compare', 'type', 'compare_key', 'type_key' ) as $key ) {
281+
foreach ( array( 'key', 'compare', 'type', 'compare_key', 'type_key', 'like_escape' ) as $key ) {
282282
if ( ! empty( $qv[ "meta_$key" ] ) ) {
283283
$primary_meta_query[ $key ] = $qv[ "meta_$key" ];
284284
}
@@ -619,9 +619,10 @@ public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' )
619619
$clause['alias'] = $alias;
620620

621621
// Determine the data type.
622-
$_meta_type = $clause['type'] ?? '';
623-
$meta_type = $this->get_cast_for_type( $_meta_type );
624-
$clause['cast'] = $meta_type;
622+
$_meta_type = $clause['type'] ?? '';
623+
$meta_type = $this->get_cast_for_type( $_meta_type );
624+
$clause['cast'] = $meta_type;
625+
$meta_like_escape = isset( $clause['like_escape'] ) ? (bool) $clause['like_escape'] : false;
625626

626627
// Fallback for clause keys is the table alias. Key must be a string.
627628
if ( is_int( $clause_key ) || ! $clause_key ) {
@@ -752,8 +753,10 @@ public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' )
752753

753754
case 'LIKE':
754755
case 'NOT LIKE':
755-
$meta_value = '%' . $wpdb->esc_like( $meta_value ) . '%';
756-
$where = $wpdb->prepare( '%s', $meta_value );
756+
if ( ! $meta_like_escape ) {
757+
$meta_value = '%' . $wpdb->esc_like( $meta_value ) . '%';
758+
}
759+
$where = $wpdb->prepare( '%s', $meta_value );
757760
break;
758761

759762
// EXISTS with a value is interpreted as '='.

0 commit comments

Comments
 (0)