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' => __( '«', 'heap' ),
'nextpagelink' => __( '»', '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 );
}
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' => __( '«', 'heap' ),
'nextpagelink' => __( '»', '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 );
}
There is actually a way to get posts from all multisite sub-sites. I do it with my "Multisite Media Display" plugin here https://wordpress.org/plugins/multisite-media-display/ .
I use it to display all media items from my multisite to ensure that any submissions meet the site requirements. Works great.
I have a similar plugin for posts, but it doesn't work with the current WP version. Been a low priority to update it.
But you can get an array of all sub-sites (although the function that does that doesn't work with WP versions prior to 4.6). Then you loop through that array, doing a post query as needed.
ADDED
Here's some code, with comments. The code will get all subsites, then switch to each subsite, where you can put in your query and output the posts in 'the loop'.
$subsites_object = get_sites(); // get the sites into an object
// convert to an array
$subsites = objectToArray($subsites_object);
// process each subsite
foreach ($subsites as $subsite) {
// get some values for later if needed
// $subsite_id is important; that switches to the subsite
$subsite_id = $subsite["blog_id"];
$subsite_name = get_blog_details($subsite_id)->blogname;
$subsite_path = $subsite["path"];
$subsite_domain = $subsite["domain"];
switch_to_blog($subsite_id);
// do your post query, then do 'the loop' to get posts
}
You could put that into a template. Or you could add code for a shortcode that will output things.
Lots of help on the googles/bings/ducks. Look at the WP code docs to find out what each thing does.