[wp-hackers] advanced taxonomy queries

Ian Dunn ian at iandunn.name
Fri Dec 21 04:22:20 UTC 2012


I haven't tested this, but you might want to give it a shot:

$fooBarQuery[ 'tax_query' ] = array(
     array(
         'taxonomy'    => 'category',
         'terms'        => 'foo',
         'field'        => 'slug',
     ),
     array(
         'taxonomy'    => 'category',
         'terms'        => 'bar',
         'field'        => 'slug',
     ),
     'relation'        => 'OR'
);
$fooBarPosts = get_posts( $fooBarQuery );

There may be a more elegant way to do it, but I'm not seeing it from a 
quick glance at the Codex. If not, then you'd need to use 
$wpdb->get_results() with a custom query.

Also, check out 
http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts 
with regards to using query_posts().



On 12/20/2012 10:04 PM, Haluk Karamete wrote:
> In this query, we get the posts in the foo cat OR the bar cat
>
> $myquery['tax_query'] = array(
>      array(
>          'taxonomy' =>  'category',
>          'terms' =>  array('foo', 'bar'),
>          'field' =>  'slug',
>      ),
> );
> query_posts($myquery);
>
> how do we change in the above snippet it so that we get the posts that
> are in both categpories? That is both in the foo cat *AND* in the bar
> cat?
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list