Arrange posts by date in front page

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;

Related posts

Leave a Reply

1 comment

  1. Add orderby and order to your arguments:

    $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,
        'orderby' => 'date',
        'order' => 'DESC'
    ) );
    

    Update

    it seems it still takes one category, lists 5 posts and then goes to another, with 5 posts etc. It takes the last posts, but they are listid one category after anoter

    Try this instead then:

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
    $i = 0;
    
    if ( $cats = get_categories() ) foreach( $cats as $cat ) :
        $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,
                'orderby' => 'date',
                'order' => 'DESC'
            )
        );
    
        if ( $cat_query->have_posts() ) : ?>
            <?php while ( $cat_query->have_posts() ) : $cat_query->the_post();
                // arrays of all the posts' IDs and dates
                $the_posts['ID'][$i] = $post->ID;
                $the_posts['date'][$i] = $post->post_date;
    
                $i++;
            endwhile;
        endif; wp_reset_postdata();
    endforeach;
    
    foreach ( $the_posts['date'] as $the_post_date ) {
        $post_dates[] = $the_post_date;
    }
    
    // sort all the posts by their dates
    array_multisort( $post_dates, SORT_DESC, $the_posts['ID'] );
    
    foreach ( $the_posts['ID'] as $the_post_id ) {
        $post_ids[] = $the_post_id;
    }
    
    $query = new WP_Query( array( 'post_type' => 'post', 'post__in' => $post_ids ) );
    
    if ( $query->have_posts() ) : ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <h3 class="cat-title"><?php $cat = get_the_category( get_the_ID() ); echo $cat[0]->name; ?></h3>
        <?php
            get_template_part( 'loop', 'index' );
        endwhile;
    endif; wp_reset_postdata();