Wordpress - Modify Taxonomy pages to exclude items in child taxonomies

I know this is an old question, but it is a bit confusing and hopefully will help someone. The reason that `$query->set doesn't work is because the query has already been parsed and now we need to also update the tax_query object also. Here is how I did it:

function my_tax_query( $query ) {
    $package_id = 12345;
    $tax_query = array(
        'taxonomy' => 'package_id',
        'terms'    => array( $package_id ),
        'field'    => 'slug',
        'operator' => 'IN',
    );
    $query->tax_query->queries[] = $tax_query; 
    $query->query_vars['tax_query'] = $query->tax_query->queries;
}
add_action( 'pre_get_posts', 'my_tax_query' );

As of Wordpress 3.7 a new action named parse_tax_query was added exactly for this purpose.

function kia_no_child_terms($wp_query) {  
  $wp_query->tax_query->queries[0]['include_children'] = 0;
}
add_action('parse_tax_query', 'kia_no_child_terms');

This hook modifies the values of both query_vars and tax_query. Using the pre_get_posts method resulted in duplicate taxonomy queries, at least for me.

Prior to 3.7 you must use the pre_get_posts action instead, as detailed in the other answers.


I could not get this to work with any combination of pre_get_posts or parse_query. I can do it relatively easily by wiping out the query object after it is made. I don't like it because then I'm running the query twice, but I'm at my wit's end with trying to be "efficient."

function kia_no_child_taxonomies(){

    if(is_tax()){
        $args = array(
            'tax_query' => array(
                array(
                    'taxonomy' => get_query_var('taxonomy'),
                    'field' => 'slug',
                    'terms' => get_query_var('term'),
                    'include_children' => FALSE
                )
            )
        );
        query_posts($args); 
    }
}

 add_action('wp','kia_no_child_taxonomies');

So until someone comes along with a better answer, this is the only method I have found so far.

EDIT:

Adapting the answer from @Tanner Moushey, I was finally able to make this work to exclude all child terms from a taxonomy archive on the pre_get_posts hook without the inefficient double-query.

function kia_no_child_taxonomies( $query ) {

    if( is_tax() ):

    $tax_obj = $query->get_queried_object();

   $tax_query = array(
                    'taxonomy' => $tax_obj->taxonomy,
                    'field' => 'slug',
                    'terms' => $tax_obj->slug,
                    'include_children' => FALSE
            );
   $query->tax_query->queries[] = $tax_query;
   $query->query_vars['tax_query'] = $query->tax_query->queries;

   endif;

}
add_action( 'pre_get_posts', 'kia_no_child_taxonomies' );