admin管理员组文章数量:1316974
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.
本文标签: wp queryMultisite how to display merged posts from two sites and sort by latest date
版权声明:本文标题:wp query - Multisite how to display merged posts from two sites and sort by latest date? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742010995a2412880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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