Get latest posts from multisite
I've created a multisite on a local VM and installed my theme.
Now I'm trying to get the latest posts from all the sites in order to display them ordered by last updated on homepage.
From what I've read so far, the best option should be creating a custom template for my home page. Is it correct? So I take the code in my template page.php but I cannot understand how to change it.
?php
/**
* The template for displaying all pages.
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that other
* 'pages' on your WordPress site will use a different template.
*
*/
get_header();
global $heap_private_post;
if ( post_password_required() ! $heap_private_post['allowed'] ) {
// password protection
get_template_part( 'theme-partials/password-request-form' );
} else { ?
div class="page-content single-page-content"
?php if ( have_posts() ): the_post(); ?
article class="article page page-single page-regular"
header
?php if ( has_post_thumbnail() ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full-size' );
if ( ! empty( $image[0] ) ): ?
div class="page__featured-image"
img src="?php echo $image[0] ?" alt="?php the_title(); ?"/
/div
?php endif;
endif; ?
/header
div class="page__wrapper"
section class="page__content js-post-gallery cf"
h1 class="page__title"?php the_title(); ?/h1
hr class="separator separator--dark"/
?php the_content(); ?
/section
?php
global $numpages;
if ( $numpages 1 ):
?
div class="entry__meta-box meta-box--pagination"
span class="meta-box__title"?php _e( 'Pages', 'heap' ) ?/span
?php
$args = array(
'before' = 'ol class="nav pagination--single"',
'after' = '/ol',
'next_or_number' = 'next_and_number',
'previouspagelink' = __( 'laquo;', 'heap' ),
'nextpagelink' = __( 'raquo;', 'heap' )
);
wp_link_pages( $args );
?
/div
?php
endif;
//comments
if ( comments_open() || '0' != get_comments_number() ):
comments_template();
endif; ?
/div
/article
?php
else :
get_template_part( 'no-results' );
endif; ?
/div!-- .page-content --
?php } // close if password protection
get_footer();
EDIT I was able to achieve it using this code:
foreach( $subsites as $subsite ) {
$subsite_id = get_object_vars($subsite)["blog_id"];
$subsite_name = get_blog_details($subsite_id)-blogname;
switch_to_blog($subsite_id);
$blog_posts = get_posts();
restore_current_blog();
foreach( $blog_posts as $post ) {
setup_postdata( $post );
}
Topic get-posts multisite query-posts Wordpress
Category Web