Exclude Child Terms From Parent Posts

How can I exclude the child term posts from showing up in the parent term posts output?? Right now it’s duplicating in both parent and child term outputs.

 //Function to display posts grouped by taxonomies
    function display_posts_per_taxonomies($parent_term, $post_type = 'beat_posts', $taxonomy = 'beats'){
    $parent_posts = get_posts(array(
    'tax_query' => array( array(
    'taxonomy' => $taxonomy,
    'field' => 'slug',
    'terms' => $parent_term
        )),
        'post_type' => $post_type
    ));
    echo '<ul>';
    foreach($parent_posts as $post){
        echo "<li>{$post->post_title}</li>"; 
    }
    $children_terms = get_terms($taxonomy, array(
        'parent' => get_term_by('slug', $parent_term, $taxonomy)->term_id
    )); 
    foreach($children_terms as $term){
        echo '<li>';
        display_posts_per_taxonomies($term->slug, $post_type, $taxonomy);
        echo '</li>';
    }
    echo '</ul>';   
}

Below is what I’m trying to get rid of.

<ul>
<li>adsf</li>
<li>ergerg</li> <---get rid of this one (duplicate)
<li>asdfasdfsdf</li> <---get rid of this one (duplicate)
<li>rthhdhdfhdhdfhdfg</li>
<li>
<ul>
<li>ergerg</li>
<li>asdfasdfsdf</li>
</ul>
</li>
</ul>

Related posts

Leave a Reply

3 comments

  1. In your get_terms() call, try setting the hierarchical option to false:

    $children_terms = get_terms($taxonomy, array(
            'parent' => get_term_by('slug', $parent_term, $taxonomy)->term_id,
            'hierarchical' => false
    ));
    

    This option normally defaults to true, which is probably why you’re getting the extra copies.

  2. I fought with this problem for three days, but in the end I won by using a custom query. Put this code in your taxonomy.php template:

    <?php
    global $wpdb;
    global $post;
    
    $term = get_term_by('slug', $wp_query->get( 'term' ), 'menu');
    
    $querystr = "
    SELECT wposts.* 
    FROM $wpdb->posts wposts
        LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
        LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->term_taxonomy.taxonomy = 'menu'
        AND $wpdb->term_taxonomy.term_id IN( $term->term_id )
    LIMIT 20";
    
     $pageposts = $wpdb->get_results($querystr, OBJECT); ?>
    
    
    <?php if( $pageposts ) : ?>
    
      <?php foreach ( $pageposts as $post ): ?>
      <?php setup_postdata($post); ?>
    
    //Echo post title/content here.
    
      <?php endforeach; endif; ?>
    

    I use the “menu” taxonomy, but you need to edit accordingly.

  3. Firstly as Bainternet says your function is never-ending.

    But to solve your original problem, there is a undocumented parameter for tax_query to prevent posts in child terms being fetched. It requires a fairly recent version of WordPress (I don’t know the exact version in which it was implemented but it certainly works in the latest release).

    Try this, it should do the trick:

    $parent_posts = get_posts(array(
        'tax_query' => array( 
            array(
                'taxonomy' => $taxonomy,
                'field' => 'slug',
                'terms' => $parent_term,
                'include_children' => 0
            )
        ),
        'post_type' => $post_type
    ));
    

    As you’ll notice the inclusion of 'include_children' => 0 will stop child posts in hierarchical taxonomies from being displayed.