Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Listing Post Type Posts with Parent and Child taxonomy categories WordPress

  • REFUNDED

A) I have a custom post type named "beat_posts".
B) I have a taxonomy named "beats" for my beat_posts.
C) I added a parent beat term named "set-up" which I add posts to.
D) I also added a child beat term under "set-up" named "chase" which I also add posts to.
E) I have a page template that I query the parent beat terms and list out the posts associated with them. So I would query "set-up" for one list. Then query another parent such as "beginning" separately. Essentially so I have a page full of different parent beat terms with their associated posts under them, on one page.

Note: there will be let's say 10+ parent beat terms (not really important probably but just so you know). Not every parent will have a child though, but for any, the child terms will be named differently from each other and will not be known before hand. So although I could run a query specifically for "set-up", I won't be able to know it's children names in the query, to hard code it.

What I'm trying to achieve is to list out all posts under "set-up", including any child posts...fair enough, that's something I can do...BUT, I need to take the child term posts of "chase" and group them together in their own unordered list within the parent unordered list. I have to keep it open to IF ANY CHILD POST is present under the parent, that it will be grouped with it's child term. It's basically laying it out like a drop down with depths does, where the child term creates a new UL in the LI. Something like this:

[SETUP POSTS]
<ul><!--START parent term "set-up"-->
<li>{set-up post}</li>
<li>{set-up post}</li>
<li>{set-up post}</li>
<li>
<ul><!--child term "chase"-->
<li>{chase post}</li>
<li>{chase post}</li>
</ul>
</li>
<li>{set-up post}</li>
<li>{set-up post}</li>
<li>
<ul><!--child term "whatever it is"-->
<li>{any other child post}</li>
<li>{any other child post}</li>
</ul>
</li>
</ul><!--END parent term "set-up"-->

Hope I make sense in my explanation.

Answers (1)

2011-05-28

Bayron Guevara answers:

Please put this code snippet into your functions.php file.

//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>';
}


Then call that function from within your template

display_posts_per_taxonomies('set-up');


Jon Stephen comments:

Bayron, thanks for your help. This seems to be really really close, however there's an issue with the posts of the children showing twice (as in the parent output), as well as the child UL repeating the main UL, which I have with an ID attached to it, instead of creating it's own individual one as a plain UL. This is how it's outputting now with your provided code:


<ul id="sortable5">
<li>adsf</li>
<li>ergerg</li>
<li>asdfasdfsdf</li>
<li>rthhdhdfhdhdfhdfg</li>
<li>
<ul id="sortable5">
<li>ergerg</li>
<li>asdfasdfsdf</li>
</ul>
</li>
</ul>


Any ideas how to make it end up like below?


<ul id="sortable5">
<li>adsf</li>
<li>rthhdhdfhdhdfhdfg</li>
<li>
<ul>
<li>ergerg</li>
<li>asdfasdfsdf</li>
</ul>
</li>
</ul>


Jon Stephen comments:

Actually, the part about the UL for the children being a plain UL I found a work around by just using the $parent_term output as the ID. So that can be disregarded but everything else still needs help.