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();
}
?>
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();
}
?>
Share
Improve this question
asked May 1, 2020 at 13:08
ianhmanianhman
135 bronze badges
4
|
1 Answer
Reset to default 0I 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.
usort()
call to sort them by date. But I don't think that would work withhave_posts()
andthe_post()
and (probably; I'd need to see the code) thetemplate-parts/content
file. – Pat J Commented May 1, 2020 at 16:51