[wp-trac] [WordPress Trac] #42409: Add LIKE support to meta_key comparisons in WP_Meta_Query

WordPress Trac noreply at wordpress.org
Thu Nov 2 01:58:09 UTC 2017


#42409: Add LIKE support to meta_key comparisons in WP_Meta_Query
-------------------------------------------------+-------------------------
 Reporter:  Otto42                               |       Owner:
     Type:  enhancement                          |      Status:  new
 Priority:  normal                               |   Milestone:  Awaiting
Component:  Database                             |  Review
 Severity:  normal                               |     Version:  trunk
 Keywords:  good-first-bug needs-unit-tests      |  Resolution:
  has-patch                                      |     Focuses:
-------------------------------------------------+-------------------------

Comment (by dd32):

 There's a few more nuances as to how people have been using `LIKE` queries
 too, some of which this will cover, some of it which it won't (or will
 cause inconsistencies in our API).

 For example, it looks like most people are passing the exact value in they
 wish to have in the `LIKE` query - that's not how our `LIKE` support
 works.  When using `LIKE` with `meta_value` compares it is effectively a
 `CONTAINS` query instead, in other words, we don't support anchored
 searches like `author\_%` we only support `%author%`.

 [attachment:42409.diff] adds just that inconsistency, the meta_key is used
 as-is and compared against in the `LIKE` expression, but the `meta_value
 LIKE` is escaped:
 {{{
 $query = new WP_Query( array(
     'meta_key' => 'key_%', 'meta_compare_key' => 'LIKE',
     'meta_value' => 'value_%', 'meta_compare' => 'LIKE'
 ) );

 echo $wpdb->remove_placeholder_escape( $query->request );
 // ... ( wp_postmeta.meta_key LIKE 'key_%' AND wp_postmeta.meta_value LIKE
 '%value\\_\\%%' ) ...

 }}}

 Ideally the behaviour between the two should remain consistent, and we
 should determine the best way to force it to be used as-is as the LIKE
 expression.
 Currently the only way to do a anchored LIKE in a meta_value is to use the
 `REGEXP` support instead.

 (cc @boonebgorges who has touched the meta query class a bunch)

--
Ticket URL: <https://core.trac.wordpress.org/ticket/42409#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list