Take the 2-minute tour ×

Here's the code I'm using to try and query a single taxonomy for one term, but exclude returned posts for that term that also belong to another.

In English, this is what I want: query 'resource-type' for 'testimonies', but not 'testimonies' that are also 'audio'.

Tips to tweak this code to get it to work?


    $testimonials_args = array(

        'post_type' => 'resource',
        'posts_per_page' => 1,
        'tax_query' => array(
        'relation' => 'AND',
                'taxonomy' => 'resource-type',
                'field' => 'slug',
                'terms' => array( 'testimonies' )
                'taxonomy' => 'resource-type',
                'field' => 'slug',
                'terms' => array( 'audio' ),
                'operator' => 'NOT IN'

share|improve this question

2 Answers 2

up vote 1 down vote accepted

I guess its because you are trying two conditions on one taxonomy, you can allways create a custom sql query, something like this:

$querystr = "
FROM $wpdb->posts
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_type = 'resource' 
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'resource-type'
AND $wpdb->terms.slug = 'testimonies'
AND $wpdb->terms.slug NOT IN ('audio')
ORDER BY $wpdb->posts.post_date DESC


or you can query just by one term and exclude by has_term() inside the loop something like this:

if (!has_term('audio','resource-type',$post->ID)){
  //post without audio
  //post with audio (skip or whatever)
share|improve this answer
No luck with that one either. Same results. –  developdaly Jun 3 '11 at 2:53
I've updated my answer –  Bainternet Jun 3 '11 at 3:18

no idea if this would work as i don't have enough custom tax terms locally to test this right now, but what if you change the AND relation to OR ?

edited b/c i was reading in the comments at : http://ottopress.com/2010/wordpress-3-1-advanced-taxonomy-queries/ that what you are asking is pretty much impossible.

however, you could maybe query all audio testimonies, spit their IDs into an array. then query all testimonies and use the array you created in the posts__not_in parameter.

share|improve this answer

Your Answer


