display merged posts from multisite and sort by latest date

The solution (with template parts):

$blogs = array( 1,4,5 );
$all_posts = array();
foreach( $blogs as $blog ) {
    switch_to_blog( $blog );
    $args = array( 'category_name' => 'noticias', 'posts_per_page' => 2, 'orderby' => 'publish_date', 'order' => 'DESC');
    $blog_posts = get_posts( $args );
    foreach( $blog_posts as $blog_post ) {
        // Correct permalink, blogname, blog url and post thumnail (with size)
        $blog_post->permalink = get_the_permalink( $blog_post->ID );
        $blog_post->blogname = get_bloginfo( $blog_post->ID, 'name'  );
        $blog_post->blogurl = get_site_url($blog_post->ID);
        $blog_post->post_thumbnail_url = get_the_post_thumbnail_url($blog_post->ID, 'noticia-archivo'); 
        $all_posts[] = $blog_post;
    }
    restore_current_blog();
}

usort( $all_posts, '__sort_by_date' );
// Display all posts
foreach( $all_posts as $post ) {
setup_postdata( $post );
// Template part
get_template_part( 'template-parts/card-1' );       
}

function __sort_by_date( $b, $a ) {
    $a_date = strtotime( $a->post_date );
    $b_date = strtotime( $b->post_date );
    if( $a_date === $b_date ) { return 0; }
    if( $a_date > $b_date ) { return 1; }
    return -1;
}

the template part:

<div class="item">
<div class="card-height--same">
<img src= <?php echo '"'.esc_url($post->post_thumbnail_url).'"'; ?> class="img-fluid" alt="">
<span class="tag-fixed"><a href="<?php echo( '' . $post->permalink . '' ); ?>" style="background: #fec60d;color: #444;"> <?php echo $post->blogname; ?> </a></span>
        <div class="uc-card_body">
          <p class="p-size--sm p-color--gray p-text--condensed"><?php the_date();?></p>
          <h4><?php the_title(); ?></h4>
          <div class="mt-auto">
<?php echo( '<a class="uc-btn btn-inline" href="' . $post->permalink . '">' ); echo( 'Ver más<i class="uc-icon">keyboard_arrow_right</i></a>' ); ?>
          </div>
        </div>
      </div>
    </div>

Leave a Comment