Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
share|improve this question
    
The terms array is not empty if I remove the 'operator' key from the query. –  fredrover Sep 17 '12 at 16:21
1  
maybe obvious, but do the posts have other terms in that taxonomy associated with them? –  Milo Sep 17 '12 at 16:43
    
Yep. The other terms show up when I run a query that doesn't exclude a term. –  fredrover Sep 17 '12 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 '12 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 '12 at 17:08

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
Please accept your own answer –  Pieter Goosen Nov 6 at 8:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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