3

I have created a custom hierarchical taxomony and when viewing a taxonomy archive page would like to only display the posts that have been assigned to that term. This works fine on the child term pages, but the parent term pages display posts assigned to them AND any child terms.

I found a solution that solves this for categories by inserting the following link after the beginning of the loop:

<? if ((is_category()) && in_category($wp_query->get_queried_object_id())) { ?>

But I haven't been able to find a similar solution that works for custom taxonomies.

I also tried:

function exclude_children($wp_query) {
    if ( isset ( $wp_query->query_vars['custom_taxomony'] ) ) {
        $wp_query->set('tax_query', array('include_children' => false));
    }
}  
add_filter('pre_get_posts', 'exclude_children'); 

But that does not seem to have any effect. So the question is, how do I do this?

  • I did not test it, but tax_query is supposed to take an array of arrays, so maybe try: $wp_query->set('tax_query', array( array('include_children' => false))); – Dave Romsey Jun 14 '12 at 20:24
  • Okay, just tried that, but no luck. With the extra array it gives me a 'Page not found' error. – Sam Jun 14 '12 at 21:27
4

Okay, I've found an answer. Part of the issue was the missing array @goto10 mentioned, and the other part was that tax_query has required arguments. Here's what I'm using so far:

function exclude_children($wp_query) {
    if ( isset ( $wp_query->query_vars['custom_taxomony'] ) ) {
        $wp_query->set('tax_query', array( array (
            'taxonomy' => 'custom_taxonomy',
            'field' => 'slug',
            'terms' => $wp_query->query_vars['custom_taxonomy'],
            'include_children' => false
        ) )
    }
}  
add_filter('pre_get_posts', 'exclude_children'); 

I'd prefer to be using a variable for taxonomy => custom_taxonomy rather than hard coding the value in as that seems like a more reusable solution, but I don't know how to pull the values from the WP_Tax_Query object.

The important takeaway from this is that taxonomy, field, and terms are all required values, though that's not clear from the Codex.

  • I never quite figured this out, so you definitely got further than I did. Can you get the taxonomy dynamically via get_query_var('taxonomy')? – helgatheviking Mar 17 '13 at 19:32
2

An empty $taxonomy_slugs array will exclude children for all taxonomy archives.

function taxonomy_archive_exclude_children($query){
   $taxonomy_slugs = ['product_category', 'application_category'];
   if($query->is_main_query() && is_tax($taxonomy_slugs)){
      foreach($query->tax_query->queries as &$tax_query_item){
        if(empty($taxonomy_slugs) || in_array($tax_query_item['taxonomy'], $taxonomy_slugs)){
            $tax_query_item['include_children'] = 0;
        }
      }
   }
 }
 add_action('parse_tax_query', 'taxonomy_archive_exclude_children');
  • I think this is great. I came from wordpress.stackexchange.com/questions/318175/… I'd love to understand a little more about it... I guess the logic is... for each queried post in a taxonomy view, apply include_children=0 ? This works great in my admin backend (ie. /wp-admin/edit.php?company=att&post_type=article ) but not on my taxonomy.php, should that be the case? – Robert Andrews Nov 1 '18 at 16:12
1

I used query_posts() to achieve this:

$args = array(
    'post_type' => 'custom_post_type',
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1), // Required if using pagination
    'tax_query' => array(
        array(
            'taxonomy' => 'custom_taxonomy',
            'field' => 'slug',
            'terms' => $term,
            'include_children' => false
        )
    )
);

query_posts( $args );

Then your loop ...

if ( have_posts() ) :  while ( have_posts() ) : the_post();

    // Your loopy stuff

endwhile;

// Include your pagination ...

the_posts_pagination(); // Or whatever you prefer

endif; wp_reset_query();

As @Sam said, taxonomy, field, and terms are all required for tax_query() (this tripped me up).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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