Nice one! GhostOne's solution was what I had been looking for.
In my situation the custom post type was 'minining_accidents' and custom taxonomies associated with this was 'accident-types' which had multiple terms under it. My idea was to create a custom widget to show list of posts under terms in this custom taxonomies. In my trial run it got what I wanted. Rest was spruce up. Here is my code:
function fn_get_list_of_mining_accident_types()
{
$custom_taxonomy='accident-types';
$custom_terms = get_terms($custom_taxonomy);
$str_return='<ul>';
foreach($custom_terms as $custom_term)
{
wp_reset_query();
$args = array(
'post_type' => 'minining_accidents',
'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>';
return $str_return;
}
Yes! There always is an option to further improve the code.