最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Multisite how to display merged posts from two sites and sort by latest date?

programmeradmin0浏览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();
}
?>

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
  • 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 Commented 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 Commented 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" – Danilo Muñoz Commented 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 Commented Nov 7, 2020 at 0:40
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论