I found this question:

Theres a way to use $query->set('tax_query' in pre_get_posts filter?

which seems to indicate that yes, you can alter the taxonomy query on taxonomy archives via pre_get_posts(). so i came up with

add_action('pre_get_posts', 'kia_no_child_terms' );

function kia_no_child_terms( $wp_query ) {  
  if( is_tax() ) {
     $wp_query->tax_query->queries[0]['include_children'] = 0;
  }
}

as well as

add_action('pre_get_posts', 'kia_no_child_terms' );

function kia_no_child_terms( $wp_query ) {
   if( is_tax() ) {
        $tax_query = $wp_query->get( 'tax_query' );
        $tax_query->queries[0]['include_children'] = 0;
    $wp_query->set( 'tax_query', $tax_query );  
    }    
}

to try to set the include_children parameter to false... and just about every combination of the two i can think of. so far however, the taxonomy archive is still showing the items in the child term

and the following test just seems to ADD the additional tax queries instead of overwriting them... which just confuses me.

function dummy_test( $wp_query){
$tax_query = array(
             'relation' => 'OR',
             array(
               'taxonomy' => 'tax1',
               'terms' => array( 'term1', 'term2' ),
               'field' => 'slug',
             ),
             array(
               'taxonomy' => 'tax2',
               'terms' => array( 'term-a', 'term-b' ),
               'field' => 'slug',
             ),
           );


$wp_query->set( 'tax_query', $tax_query );

);
add_action('pre_get_posts','dummy_test');

shouldn't SET overwrite the current value?

link|improve this question

77% accept rate
Please take a look at this answer. That should bring you further. – kaiser Feb 23 at 7:00
thanks, but that is pretty much what i have tried. var_dumps/print_rs of the $wp_query global are showing that new tax query in addition to the existing query instead of in place of... at least on my taxonomy page. – helgatheviking Feb 23 at 15:52
Yea, that code is meant to add to the query. – kaiser Feb 23 at 16:05
so there is no way to adjust/override the existing query? b/c what i want is to change the include_children parameter – helgatheviking Feb 23 at 16:11
feedback

1 Answer

up vote 0 down vote accepted

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 b/c 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 w/ a better answer, this is the only method i have found so far.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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