[wp-trac] [WordPress Trac] #20312: Meta_query needs relation arg per key/value pair

WordPress Trac wp-trac at lists.automattic.com
Wed Mar 28 10:06:10 UTC 2012


#20312: Meta_query needs relation arg per key/value pair
-------------------------+-----------------------------
 Reporter:  CoenJacobs   |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Query        |    Version:  3.2
 Severity:  normal       |   Keywords:  dev-feedback
-------------------------+-----------------------------
 Since #17011 introduced the relation param to meta_query args, it is
 possible to set it to 'AND' or 'OR' to define how the multiple key/value
 pairs should be related to each other. This can only be set for the entire
 meta_query array, so it will be applied to all key/value pairs.

 Imagine the following meta_query array, where I want to get all posts
 where '_kind' is 'value_1' or 'value_2'. Either way, '_exclude' should
 ALWAYS be '-1'. This array will not work, since if any of the key/value
 pairs match, that post will be returned:

 {{{
 [meta_query] => Array
     (
         [0] => Array
             (
                 [key] => _kind
                 [value] => value_1
             )

         [1] => Array
             (
                 [key] => _kind
                 [value] => value_2
             )

         [2] => Array
             (
                 [key] => _exclude
                 [value] => -1
             )

         [relation] => OR
     )
 }}}

 So, I had to think of a way to set the relation per key/value pair. This
 would involve adding an extra array inside the meta_query array. Each
 group of key/value pairs can have its own 'relation' param, making the
 meta_query able to properly structure the query. Meta_query array will
 look something like this:

 {{{
 [meta_query] => Array
     (
         [0] => Array (

             [relation] => OR
             [0] => Array
                 (
                     [key] => _kind
                     [value] => value_1
                 )

             [1] => Array
                 (
                     [key] => _kind
                     [value] => value_2
                 )
             )

         [1] => Array (
             [relation] => AND
             [0] => Array
                 (
                     [key] => _exclude
                     [value] => -1
                 )
             )

         [relation] => AND
     )
 }}}

 Haven't figured out what would need to be changed to the WP_Query and
 WP_Meta_Query classes to support  this change, but that shouldn't involve
 anything more than an extra loop (easy to port for backwards compat). Only
 thing I'm worried is what this will do to performance.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/20312>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list