0

I'm using multisite and pulling blog posts from 2 sites.

The below query displays the posts but does not merge the posts together, it just lists all of site 1 posts followed by site 2 posts and therefore the posts/dates are all out of order. At the moment it looks like:

Site 1 | Post A | 10 April 2020

Site 1 | Post B | 05 February 2020

Site 2 | Post C | 01 May 2020

Site 2 | Post D | 10 April 2020

What I want to achieve is have both site's posts displayed together ordered by latest date? How to do this? For example it should look like this (latest posts shown first regardless of site)

Site 2 | Post C | 01 May 2020

Site 1 | Post A | 10 April 2020

Site 2 | Post D | 10 March 2020

Site 1 | Post B | 05 February 2020

<?php
$blog_ids = array( 1, 2 );

foreach( $blog_ids as $id ) {

switch_to_blog( $id );

$args = array(
    'category_name' => 'direct, uncategorized',
    'post_status' => 'publish',
    'orderby' => 'publish_date',
    'order' => 'DESC',
    'posts_per_page' => '10',
);


$query = new WP_Query( $args );


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

<?php   
    get_template_part('template-parts/content', 'blog');
?>

<?php
endwhile;
endif;

wp_reset_postdata();

restore_current_blog();
}
?>
4
  • You could merge the post lists from the two queries into a single array, then use a usort() call to sort them by date. But I don't think that would work with have_posts() and the_post() and (probably; I'd need to see the code) the template-parts/content file.
    – Pat J
    May 1, 2020 at 16:51
  • @PatJ thanks. I found a solution posted below. It's not exactly what I was looking for but it works nonetheless.
    – ianhman
    May 1, 2020 at 18:40
  • i have the same problem. every site have the category "noticias" but, like the original problem, don't display posts together ordered by latest date my 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" Nov 7, 2020 at 0:13
  • 1
    @DaniloMuñoz if you have a question post it as a new question or a comment, don't post it as an answer
    – Tom J Nowell
    Nov 7, 2020 at 0:40

1 Answer 1

0

I found a solution here. It works but it's not ideal as I can't use template-parts files etc. If anyone knows of a solution where I can use template-parts it would be much appreciated.

2
  • The problem is that the template functions (like have_posts(), the_title(), the_permalink(), etc) all assume that the posts in the query are all coming from the same site. You want posts from multiple sites, which means that the assumptions made by the template functions will be incorrect. (As an aside: double-check that your posts all have correct URLs in your solution.)
    – Pat J
    May 1, 2020 at 19:11
  • 1
    @PatJ. I understand. Posts URLs are working correctly :)
    – ianhman
    May 1, 2020 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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