[wp-trac] [WordPress Trac] #27344: Array support in WP_Meta_Query when compare operator is LIKE or NOT LIKE

WordPress Trac noreply at wordpress.org
Mon Mar 10 22:40:03 UTC 2014


#27344: Array support in WP_Meta_Query when compare operator is LIKE or NOT LIKE
-------------------------+-----------------------------
 Reporter:  digitty      |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Query        |    Version:  trunk
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 = Usage scenario =

 Consider a lead custom post type with several custom fields (including
 '''contact name''' and '''company name''') and a search form (with fields
 for each of the supported custom fields). Contact name and company name
 may contain multiple words and it should be possible to search for one or
 more words. The search form has an extra criteria `<select>` field **''
 for each custom field **'', allowing to search for '''ANY''' or '''ALL'''
 entered words.

 = Related tickets =

 Ticket #18707 is related to the enhancement discussed here but I thought
 of a more general approach: it should be possible to specify the operator
 between each LIKE / NOT LIKE comparison, to be either '''AND''' or
 '''OR'''.

 This would allow specifying the ANY or ALL filter mentioned above without
 needing to edit the SQL clauses directly through `posts_clauses` filter:

 {{{
 (wp_postmeta.meta_key = 'cf_contact_name' AND (wp_postmeta.meta_value LIKE
 '%bob%' AND wp_postmeta.meta_value LIKE '%rob%'))
 AND
 (mt1.meta_key = 'cf_company_name' AND (mt1.meta_value LIKE '%com%' OR
 mt1.meta_value LIKE '%net%'))
 }}}

 = Changes to WP_Meta_Query =

 The above SQL query excerpt should be possible with the following
 `meta_query` syntax:

 {{{
 'meta_query' => array(
     'relation' => 'AND',
     array(
         'key'     => 'cf_contact_name',
         'value'   => array( 'AND' => array('bob', 'rob') ),  // ALL filter
         'compare' => 'LIKE',
         'type'    => 'CHAR',
     ),
     array(
         'key'     => 'cf_company_name',
         'value'   => array( 'OR' => array('com', 'net') ),   // ANY filter
         'compare' => 'LIKE',
         'type'    => 'CHAR',
     ),
 ),
 }}}

 The patch file for `meta.php` that will allow this is attached.

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


More information about the wp-trac mailing list