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>
In your
get_terms()
call, try setting thehierarchical
option tofalse
:This option normally defaults to
true
, which is probably why you’re getting the extra copies.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:
I use the “menu” taxonomy, but you need to edit accordingly.
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:
As you’ll notice the inclusion of
'include_children' => 0
will stop child posts in hierarchical taxonomies from being displayed.