[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 11:04:19 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              |  Resolution:
 Keywords:  dev-feedback close  |
--------------------------------+------------------------------
Changes (by scribu):

 * keywords:  dev-feedback => dev-feedback close


Comment:

 When you reach a certain level of complexity, you're better off using SQL
 directly:

 {{{
 function my_meta_query( $clauses, $wp_query ) {
   global $wpdb;

   if ( $wp_query->get( 'my_specific_meta_query' ) ) {
     $clauses['join'] .= "
       INNER JOIN $wpdb->postmeta m_kind ON (ID = m_kind.post_id AND
 meta_key = '_kind')
       INNER JOIN $wpdb->postmeta m_other ON (ID = m_other.post_id AND
 meta_key = '_other')
       INNER JOIN $wpdb->postmeta m_third ON (ID = m_third.post_id AND
 meta_key = '_third')
     ";

     $clauses['where'] .= " AND (m_kind.meta_value = 'value_1' AND
 m_other.meta_value = 'value_1') OR (m_third.meta_value >
 m_kind.meta_value)
   }

   return $clauses;
 }
 }}}

 It's a little more work to set up the JOINs, but you end up with more
 readable code.

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


More information about the wp-trac mailing list