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

WordPress Trac noreply at wordpress.org
Sun Nov 12 16:52:26 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:  4.9
 Keywords:  good-first-bug needs-unit-tests      |  Resolution:
  has-patch                                      |     Focuses:
-------------------------------------------------+-------------------------

Comment (by joshf):

 attachment:42409.2.diff is a good start, and I'll echo @dd32's comments on
 anchored searches, particularly on the meta_key.

 I've seen anchored meta_key searches on more than one client site, and
 it's not all that uncommon to see on sites using ACF. For example:

 {{{#!php
 $cpt_ids = new WP_Query( array(
     'post_type'       => 'cpt',
     'meta_query'      => array(
         array(
             'key'     => 'styles_%_style_id', // ACF likes to place an
 unique identifier
                                               // in the middle of the key
 name.
                                               // So let's match all keys,
 regardless
                                               // of that unknown, unique
 identifier.
             'value'   => $style_id,
             'compare' => '=',
         )
     ),
 ) );
 }}}

 This query returns zero results in 4.8.3.

 Using attachment:42409.2.diff, the meta_key search has to be simplified in
 order to make that query work. (And then hope the meta_key search isn't
 overly vague.)

 {{{#!php
 $cpt_ids = new WP_Query( array(
     'post_type'       => 'cpt',
     'meta_query'      => array(
         array(
             'key'     => 'styles',
             'value'   => $style_id, // This is an unique value.
             'compare' => 'LIKE',
             'compare_key' => 'LIKE'
         )
     ),
 ) );
 }}}

 It's a workaround for now, but I can foresee potential issues with
 simplifying the meta_key search in this way. I like the idea of allowing
 an 'as-is' option as @mariovalney suggested. That would allow more
 advanced(?) queries to continue utilizing the WP_Query class.

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


More information about the wp-trac mailing list