How to Get a Post from Another Site in Multisite Network
Let’s assume, that you’re currently working on Site A and you need to get and display a post (or page or a custom post type – doesn’t matter) from Site B of your Multisite network.
How to do that?
It can be achieved easily – with the help of two WordPress functions – switch_to_blog()
and restore_current_blog()
. Just like this:
switch_to_blog( $site_B ); // site ID is here
$post = get_post( $post_id );
restore_current_blog(); // switch to PREVIOUS site
You can even create a custom function for this purpose:
function get_site_post( $post_id, $site_id ) {
switch_to_blog( $site_id );
$post = get_post( $post_id ); // get a post from another blog
restore_current_blog();
return $post;
}
But please keep in mind that these two functions are only affect the database. So, when you switch to Site B, you can use all the WordPress functions like get_posts()
, get_terms()
, wp_insert_post()
etc to work with the database of Site B, but you will not be able to use functions and methods of plugins that are active on Site B. For example, if WooCommerce plugin is installed on Site B only, you will not gain the access to wc_get_product()
function. It is just worth to remember.
Let’s assume that we need to display posts from multiple blogs within a Multisite network. You can use the code below in a custom page template for example.
$current_blog_id = get_current_blog_id();
$sites = get_sites(
array(
'number' => 50,
'site__not_in' => $current_blog_id, // exclude current site from the loop
)
);
foreach( $sites as $site ) {
switch_to_blog( $site->blog_id );
// get posts from other blogs (the latest 10)
$blog_posts = get_posts( array( 'posts_per_page' => 10 ) );
foreach( $blog_posts as $blog_post ) {
echo "<h2>{$blog_post->post_title}</h2>";
}
restore_current_blog();
}
Guess what happens when you use switch_to_blog()
and restore_current_blog()
functions inside the loop? Yes, the performance impact can be huge, especially if you have hundreds of sites in your Multisite network.
You could even try to optimise your loop performance by skipping restore_current_blog()
function usage. So, no need to switch to a previous site on every loop iteration, when you can just switch to the original blog after the loop ends.
echo "<h2>{$blog_post->post_title}</h2>";
}
}
switch_to_blog( $current_blog_id );
$GLOBALS[ '_wp_switched_stack' ] = array();
$GLOBALS[ 'switched' ] = false;
A much better way of course is to use custom SELECT
queries. But please, just look at the example below – only 4 lines of code and no switch_to_blog()
usage.
$blog_posts = new Network_Query( array( 'posts_per_page' => 10 ) );
while( $blog_posts->have_posts() ) : $blog_posts->the_post();
echo "<h2>{$blog_posts->post->post_title}</h2>";
}
Of course Network_Query
is not available in WordPress MU by default, it is a part of my plugin. You can install it and enjoy 🙃

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Awesome