Excluding a custom taxonomy term breaks wp_get_post_terms

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(
                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

Related posts

Leave a Reply

1 comment

  1. Thanks for your help everybody.

    I was using a custom post type and forgot to include

    'post_type'=>'my_custom_post_type' 
    

    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.