0

i have a code. it works, but not perfectly: every site have the category "noticias" but don't display posts together ordered by latest date, only as a sequence (blog 1: 3 post order by date, blog 2: 3 post order by date, blog 3: 3 post order by date, etc.) and i want to display all together.

the site: the yellow are from blogs 2-3

enter image description here

the code

$blog_ids = array( 1, 2, 3, );
foreach( $blog_ids as $id ) {
switch_to_blog( $id );
$args = array(
'category_name' => 'noticias',
'post_status' => 'publish',
'orderby' => 'publish_date',
'order' => 'DESC',
'posts_per_page' => '4',
);

$query = new WP_Query( $args );

if( $query->have_posts() ) :
while( $query->have_posts()) : $query->the_post() ; 
?>

<div class="col-md-3 mb-36">
<div class="uc-card card-height--same">

<span class="tag-fixed"><?php $tags = get_the_tags($post->ID);  ?>
            <?php foreach($tags as $tag) :  ?><a class="uc-tag" href="<?php bloginfo('url');?>/tag/<?php print_r($tag->slug);?>"><?php print_r($tag->name); ?></a> <?php endforeach; ?></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="uc-text-divider divider-primary my-20"></div>
  <div class="mt-auto">
    <a href=" <?php echo get_permalink(); ?> " class="uc-btn btn-inline">
      Ver más
      <i class="uc-icon">keyboard_arrow_right</i>
    </a>
      </div>
    </div>
  </div>
</div>

<?php
endwhile;
endif;
wp_reset_postdata();
restore_current_blog();}

I hope you can help me, greetings!

2
0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.