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

WordPress Trac noreply at wordpress.org
Wed Nov 1 22:52:07 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 Review
Component:  Database     |    Version:  trunk
 Severity:  normal       |   Keywords:  good-first-bug needs-patch needs-
  Focuses:               |  unit-tests
-------------------------+-------------------------------------------------
 So, it seems that a fair amount of code, custom snippets or code in
 plugins, is making changes to WordPress SQL queries directly.

 The most common case that is causing people issues with 4.8.3 seems to be
 to allow for meta_key LIKE 'some_%_thing' or similar cases. Some of these
 come from plugins that allow for dynamically numbers of meta_keys, having
 the keys in a name_(number)_thing pattern.

 From the samples I've seen of custom code doing this (again, largely
 snippets), the most common pattern seems to be to hook into posts_where,
 look for "meta_key = 'thing_%'" and then to str_replace it with a LIKE.

 We should support this type of LIKE clause without having people resort to
 ill-advised string replacements in their SQL queries or query fragments
 directly.

 Adding support for this is relatively simple. We add a new clause that can
 be passed to the meta_query array, in the form of "meta_key_compare" and
 allowing it to be "LIKE" with it defaulting to "=".

 There are two lines that would be needed to be changed to allow for the
 LIKE to be used, and those would be lines 520 and 564 of class-wp-meta-
 query.php. Additional code would be needed to support the new argument,
 and since that argument would be in the $clauses variable, then it should
 be relatively straightforward.

 There may be performance issues associated with using meta_key_compare =>
 LIKE, however, the same performance issues exist for those cases where
 people are modifying the query already, and no such issues exist for core,
 which would likely not use the new argument.

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


More information about the wp-trac mailing list