While I’ve been able to get this working for normal WP categories I’ve been unable to get it working for custom taxonomies.
I’d like to loop through each custom taxonomy (categories in my case) and produce a number of posts for each.
An example of the output would be:
Category 1
post from category one
post from category one
read more category one
Category 2
post from category two
post from category two
read more category two
Of course it would repeat through any available taxonomy for the custom post type.
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.