Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Like the title says I have custom post types and custom taxonomy for that type.
For the Taxonomy I have terms in 3 levels like this:

  • -> Term 1
  • --> Term 1a
  • --> Term 1b
  • -> Term 2
  • --> Term 2a
  • --> Term 2b
  • ---> Term 2b-1
  • ---> Term 2b-2

Now when I visit the page for Term 2, it shows all the posts under that Term. But how to make these posts as nested list corresponding to those levels? And also showing the title for each. So the list would look something like this:

<ul>
    <li>Term 1:
        <ul>
            <li>Post 1</li>
            <li>Post 2</li>
        </ul>
    </li>
    <li>Term 2:
        <ul>
            <li>Post 3</li>
            <li>Term 3:
                <ul>
                    <li>Post 4</li>
                    <li>Post 5</li>
                </ul>
            </li>
            <li>Post 6</li>
        </ul>
    </li>
</ul>

I'm a bit lost as the query has happened already and all the needed posts are returned so I probably shouldn't make another call.

Can I format this list using the info from the query or do I need to get all my taxonomy terms and then do for each finding children and corresponding posts?
And of course I don't want duplicate posts for each level.

share|improve this question
add comment

1 Answer

May be not the best solution, but I managed to get what I need and it works. I could have added to output html ul and li tags too to create the list as I asked in the beginning, but this is OK for me and no more styling needed for this design.

            $queried_taxonomy_name = $wp_query->queried_object->taxonomy;
            $queried_term_id = $wp_query->queried_object_id;
            $queried_term = get_term_by(' id', $queried_term_id, $queried_taxonomy_name );

            function show_term_posts( $queried_posts, $term, $taxonomy ){
                $term_children = get_term_children( $term->term_id, $taxonomy );
                global $queried_term_id;
                if ( $term->term_id != $queried_term_id ){
                    echo '<h2>' . $term->name . '</h2>';
                    echo wpautop( $term->description );
                }

                $queried_post_ids = array();
                foreach ($queried_posts as $post) {
                    $queried_post_ids[] = $post->ID;
                }
                unset($post);

                $childposts = array();
                $child_post_ids = array();
                $posts_to_return_ids = array();

                if( $term_children ){
                    $childposts = array();
                    $child_post_ids = array();
                    $posts_to_return_ids = array();
                    foreach ($term_children as $queried_term_child) {
                        $term_child = get_term_by('id', $queried_term_child, $taxonomy );
                        if( $term_child->count > 0 ){
                            $args = array(
                                'post_type' => 'bk_menu',
                                'nopaging'  => true,
                                'tax_query' => array(
                                    array(
                                        'taxonomy' => $taxonomy,
                                        'field' => 'id',
                                        'include_children' => true,
                                        'terms' => array($queried_term_child)
                                    )
                                )
                            );
                            $childposts = array_merge( $childposts, get_posts( $args ) );
                        }
                    }
                    unset($queried_term_child);

                    foreach ($childposts as $child_post) {
                        $child_post_ids[] = $child_post->ID;
                    }
                    unset($child_post);

                    $child_post_ids = array_unique( $child_post_ids );
                    $posts_to_return_ids = array_diff( $queried_post_ids, $child_post_ids );

                } else {
                    $posts_to_return_ids = $queried_post_ids;
                }

                if( $posts_to_return_ids ){
                    $posts_to_return = get_posts( array(
                        'post_type' => 'bk_menu',
                        'nopaging' => true,
                        'post__in' => $posts_to_return_ids
                    ) );
                }

                if($posts_to_return){
                    global $post;
                    foreach ( $posts_to_return as $post ) {
                        setup_postdata($post);
                        ?><div class="post"><? the_title(); the_content(); ?></div><?
                    }
                    unset($post);
                    wp_reset_postdata();
                }
                if($term_children){
                    foreach ($term_children as $term_child_id) {
                        $term_child = get_term_by('id', $term_child_id, $taxonomy );
                        if( $term_child->count > 0 && $term_child->parent == $term->term_id ){
                            $args = array(
                                'post_type' => 'bk_menu',
                                'nopaging'  => true,
                                'tax_query' => array(
                                    array(
                                        'taxonomy' => $taxonomy,
                                        'field' => 'id',
                                        'include_children' => true,
                                        'terms' => array($term_child_id)
                                    )
                                )
                            );
                            $posts_to_return = get_posts( $args );
                            show_term_posts( $posts_to_return, $term_child, $taxonomy );
                        }
                    }
                }
            }

            show_term_posts( $posts, $queried_term, $queried_taxonomy_name );
share|improve this answer
add comment

Your Answer

 
discard

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.