I had an idea of combining the first code with this solution that lists posts from a specifik taxomony:
<?php $terms = get_terms('productcategories');
foreach ($terms as $term) {
$wpq = array (
'taxonomy'=>'productcategories',
'term'=>$term->slug,
'order'=>'asc',
'orderby'=>'title');
$query = new WP_Query ($wpq);
echo "$term->name:<br />";
?>
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<a href="https://wordpress.stackexchange.com/questions/123059/<?php the_permalink();?>"><?php the_title();?></a>,
<?php endwhile; endif; wp_reset_query(); ?>
<?php
echo "<br />";
}
?>
I managed to combine the two, so this is my current solution. Maybe not the best but it works.
<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
$wpq = array (
'taxonomy'=>$taxonomyName,
'term'=>$term->slug,
'order'=>'asc',
'orderby'=>'title');
$query = new WP_Query ($wpq);
echo "$term->name:<br />";
?>
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<a href="https://wordpress.stackexchange.com/questions/123059/<?php the_permalink();?>"><?php the_title();?></a>,
<?php endwhile; endif; wp_reset_query(); ?>
<?php
echo "<br />";
}
?>