I'm trying to exclude one custom taxonomy from an archive page, and then show the terms of the returned posts.

When I run WP_Query to exclude the custom term, it returns the posts correctly, but no longer fetches the terms array when using wp_get_post_terms(). I suspect that not querying in the positive prevents it from fetching the terms. Here's what my code looks like:

$query = new WP_Query( 
        array( 'tax_query' => array(
                    'taxonomy' => 'documenttype',
                    'field' => 'slug',
                    'terms' => array('pressreleases'),
                    'operator' => 'NOT IN'

            ) ) 
//then later, in the loop ...
$terms = wp_get_post_terms($query->post->ID,'documenttype');
// var_dump($terms) shows an empty array
  • The terms array is not empty if I remove the 'operator' key from the query.
    – fredrover
    Sep 17, 2012 at 16:21
  • 1
    maybe obvious, but do the posts have other terms in that taxonomy associated with them?
    – Milo
    Sep 17, 2012 at 16:43
  • Yep. The other terms show up when I run a query that doesn't exclude a term.
    – fredrover
    Sep 17, 2012 at 23:01
  • any plugins or filters in the theme that may be modifying the results? your query works as expected with wp_get_post_terms in Twenty Eleven and the Codex code for adding a taxonomy. try to eliminate any other factors.
    – Milo
    Sep 18, 2012 at 0:37
  • Hm. In the Twenty Eleven theme with a fresh install, I get even stranger results. Querying in the negative ('NOT IN') brings no results at all, when querying in the positive brings in posts in the terms requested.
    – fredrover
    Sep 19, 2012 at 17:08

1 Answer 1


Thanks for your help everybody.

I was using a custom post type and forgot to include


I finally got to the problem by doing a var_dump on the WP_Query object and looking at the SQL. Running the query directly on the database and picking through it, I eventually found my error.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.