I have code to show the 5 last posts from every category on the front page. However, at the moment they are ordered category by category. Is it possible to order them by post date, but still have 5 posts per category?
Here is my current code:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if( $cats = get_categories() ) foreach( $cats as $cat ) :
//go through all site's categories and get up to 5 posts each////
$cat_query = new WP_Query( array(
'post__not_in' => get_option( 'sticky_posts' ),
'category__in' => array($cat->term_id),
'posts_per_page' => 5,
'paged' => $paged
) );
if($cat_query->have_posts()) : ?>
<h3 class="cat-title"><?php echo $cat->name; ?></h3>
<?php while ($cat_query->have_posts()) : $cat_query->the_post();
get_template_part( 'loop', 'index' );
endwhile; endif; wp_reset_postdata();
endforeach;
Add
orderby
andorder
to your arguments:Update
Try this instead then: