[wp-trac] [WordPress Trac] #12891: Advanced multi-taxonomy WP_Query()s

WordPress Trac wp-trac at lists.automattic.com
Fri Nov 19 20:38:09 UTC 2010


#12891: Advanced multi-taxonomy WP_Query()s
----------------------------+-----------------------------------------------
 Reporter:  markjaquith     |       Owner:  markjaquith    
     Type:  task (blessed)  |      Status:  assigned       
 Priority:  normal          |   Milestone:  3.1            
Component:  Query           |     Version:  3.0.1          
 Severity:  normal          |    Keywords:  ongoing-project
----------------------------+-----------------------------------------------

Comment(by Otto42):

 When trying to create a query to do what I want (namely gallery-category
 OR gallery-post_format), I eventually came up with this:
 `query_posts(array(
         'tax_query' => array(
                 array(
                         'taxonomy' => 'post_format',
                         'terms' => array('post-format-gallery'),
                         'field' => 'slug',
                 ),
                 array(
                         'taxonomy' => 'category',
                         'terms' => array('gallery'),
                         'field' => 'slug',
                 ),
         )
 ) );
 `

 But of course can find no way to make that an OR relationship. In looking
 at the resulting query this produces (eventually), I have this:

 `SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts INNER JOIN
 wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
 INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)
 WHERE 1=1 AND wp_term_relationships.term_taxonomy_id IN (110) AND
 tt1.term_taxonomy_id IN (11) AND wp_posts.post_type IN ('post', 'page',
 'attachment') AND (wp_posts.post_status = 'publish' OR
 wp_posts.post_author = 1 AND wp_posts.post_status = 'private') GROUP BY
 wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10`

 That naturally produces zero rows, because it's assuming an AND
 relationship. The key part of that turns out to be here:
 `wp_term_relationships.term_taxonomy_id IN (110) AND tt1.term_taxonomy_id
 IN (11)`

 Simply changing that AND to an OR and grouping them in parentheses gets me
 the results I want.

 So perhaps we can add the ability of grouping to this, somehow? If I was
 to group the two things I wanted OR'ed into their own array with some kind
 of extra parameter to specify an OR relationship between them? Something
 like this:

 `query_posts(array(
         'tax_query' => array(
                 array('relationship'=>'OR',
                         array(
                                 'taxonomy' => 'post_format',
                                 'terms' => array('post-format-gallery'),
                                 'field' => 'slug',
                         ),
                         array(
                                 'taxonomy' => 'category',
                                 'terms' => array('gallery'),
                                 'field' => 'slug',
                         ),
                 );
         )
 ) );
 `

 Or something? This array nesting is bloody complicated though, there has
 to be an easier way to allow this sort of thing.

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


More information about the wp-trac mailing list