Sort posts by custom taxonomy name

One solution would be-

$terms = get_terms('taxonomy-name');
foreach($terms as $term) {
    $posts = get_posts(array(
            'post_type' => 'custom_post_type_name',
            'tax_query' => array(
                array(
                    'taxonomy' => 'taxonomy-name',
                    'field' => 'slug',
                    'terms' => $term->slug
                )
            ),
            'numberposts' => -1
        ));
    foreach($posts as $post) {
        // do what you want to do with the posts here
    }
}

Leave a Comment