WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

<li>ergerg</li> <---get rid of this one (duplicate)
<li>asdfasdfsdf</li> <---get rid of this one (duplicate)
share|improve this question
Your function is calling it self in a none recursive manner which would result in an never ending loop. – Bainternet Jun 1 '11 at 6:42

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.

share|improve this answer
I tried your suggestion Dougal, but no luck. – JonnyPlow Jun 1 '11 at 21:45

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:

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.

share|improve this answer

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( 
            '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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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