Wordpress - Loop through custom taxonomies and display posts
I thought I would provide another answer as the above one is a little hacky, also I added another layer that gets all taxonomies for a post_type.
$post_type = 'post';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" );
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
//Do you general query loop here
endwhile; endif;
endforeach;
endforeach;
It would be recommended to add each post found to a $post__not_in
array, so you could pass that to the WP_Query
to prevent duplicate posts coming through.