Cat

-Post 1

-Post 2

--Sub Cat

---Post 1

---Post 2

----Sub Sub

-----Post 1

-----Post 2

like this?

$custom_taxonomy='mytaxonomy';

$custom_terms = get_terms($custom_taxonomy);    
$str_return='<ul>';
foreach($custom_terms as $custom_term) 
{
    wp_reset_query();
    $args = array(
        'post_type' => 'myposttype',
        'posts_per_page'=> -1,
        'tax_query' => array(               
            array(
                'taxonomy' => $custom_taxonomy,
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
    );  

    $loop = new WP_Query($args);

    $term_name=$custom_term->name;
    $term_slug=$custom_term->slug;
    $term_link=get_term_link($term_slug, $custom_taxonomy);

    $str_return.='<li><a href="'.$term_link.'">'.$term_name.'</a>';

    if($loop->have_posts()) 
    {
        $str_return.='<ol>';

        while($loop->have_posts()) : $loop->the_post();
            $str_return.='<li><a href="'.get_permalink().'">'.get_the_title().'</a></li> ';
        endwhile;

        $str_return.='</ol>';           
     }
     $str_return.='</li>';
}
$str_return.='</ul>';
echo $str_return;

I have tried this, but not sure how to keep the hierarchy order

0

There are 0 best solutions below