Modify Taxonomy pages to exclude items in child taxonomies

Question

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?

0
, helgatheviking 10 years 2012-02-22T14:04:36-05:00 0 Answers 65 views 0

Leave an answer

Browse
Browse