title: Wordpress: How can I exclude posts in child taxonomies from a custom taxonomy query?
body: <p>My WordPress theme has a custom taxonomy called "Collections". The custom taxonomy is hierarchical, so there are subcollections.</p>
  
  <p>I have a Collection called "Books" and a sub-collection called "Novels". There are some posts that are just in "Books", and some posts that are in "Novels". I want the page for the "Books" collection to only show posts in the main "Books" collection, not the ones in the "Novels" subcollection. But by default, WordPress includes posts in "subcollections" in the query for a taxonomy.</p>
  
  <p>How do I exclude posts in child terms from my taxonomy query? This is easy with categories, but it seems there is no built in way to do this with custom taxonomies.</p>
  
  <hr>
  
  <p><strong>Update:</strong>
  Jan's solution worked perfectly. Here is the code I used, placed above the Loop in index.php:</p>
  
  <pre><code>// if is taxonomy query for 'collections' taxonomy, modify query so only posts in that collection (not posts in subcollections) are shown.
  if (is_tax()) {
   if (get_query_var('collection')) {
    $taxonomy_term_id = $wp_query-&gt;queried_object_id;
    $taxonomy = 'collection';
    $unwanted_children = get_term_children($taxonomy_term_id, $taxonomy);
    $unwanted_post_ids = get_objects_in_term($unwanted_children, $taxonomy);
  
    // merge with original query to preserve pagination, etc.
    query_posts( array_merge( array('post__not_in' =&gt; $unwanted_post_ids), $wp_query-&gt;query) );
   }
  }
  </code></pre>