[wp-hackers] Grouping taxonomy queries

Scott Taylor scott.c.taylor at mac.com
Wed Aug 8 03:31:36 UTC 2012


You have to do a join for every condition, so tax_query probably isn't the best mechanism. Here's how the query will end up:

SELECT ID FROM wp_posts p
INNER JOIN wp_term_relationships AS tt1 ON (p.ID = tt1.object_id) // for your IN condition *or* AND condition
INNER JOIN wp_term_relationships AS tt2 ON (p.ID = tt2.object_id) // for your AND condition
WHERE 
// IN condition *or* AND condition
( ( tt1.object_id IN ( $a_tt_id ) ) OR ( tt1.object_id IN ( $b_tt_id ) AND tt2.object_id IN ( $c_tt_id ) )

// this isn't the whole query, you'll want to check post_status, etc

So, if you can grab the term_taxonomy_ids you need first, you can do $wpdb->get_col with a query like above and then do a WP_Query with 'post__in' with the get_col result array

-- 
Scott Taylor
Musician / Software Engineer
160 West End Ave #2H 
New York, NY 10023
m 646.715.7347
twitter: wonderboymusic


On Tuesday, August 7, 2012 at 10:53 PM, John Blackbourn wrote:

> I have a situation where I'd like to perform a tax_query for posts in term
> A OR posts in term B AND C. Written another way:
> 
> Posts in ( term A ) OR ( term B AND term C )
> 
> Terms A, B and C are in different taxonomies, although from a technical
> point of view this is not relevant.
> 
> What I'm really asking for is to be able to group two of the tax queries
> (for terms B and C, using AND as the operator parameter) and then use OR as
> the operator parameter between it and the tax query for term A. This is not
> possible with the current tax query structure.
> 
> Has anyone tackled this or something similar before? It looks to me like
> I'll need to drop the tax query and filter the SQL in order to achieve what
> I want.
> 
> Any suggestions, code samples, etc most welcome.
> 
> John
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com (mailto:wp-hackers at lists.automattic.com)
> http://lists.automattic.com/mailman/listinfo/wp-hackers
> 
> 
> 




More information about the wp-hackers mailing list