Get posts from sites in Multisite?

The WordPress function switch_to_blog() expects an integer as an input parameter. You can read more about it in the Codex:

http://codex.wordpress.org/Function_Reference/switch_to_blog

Please try this kind of structure instead:

// Get the current blog id
$original_blog_id = get_current_blog_id(); 

// All the blog_id's to loop through
$bids = array( 1, 2 ); 

foreach( $bids as $bid )
{
    // Switch to the blog with the blog_id $bid
    switch_to_blog( $bid ); 

    // ... your code for each blog ...
}

// Switch back to the current blog
switch_to_blog( $original_blog_id ); 

Update:

If you want to fetch posts from different categories for each blog, you can use for example:

// Get current blog
$original_blog_id = get_current_blog_id(); 

// Setup a category slug for each blog id, you want to loop through - EDIT
$catslug_per_blog_id = array( 
    1 => 'video',
    4 => 'news' 
); 

foreach( $catslug_per_blog_id as $bid => $catslug )
{
    // Switch to the blog with the blog id $bid
    switch_to_blog( $bid ); 

    // ... your code for each blog ...
    $myposts = get_posts( 
        array( 
            'category_name'  => $catslug,
            'posts_per_page' => 10, 
        )
    );
    // ... etc
}

// Switch back to the current blog
switch_to_blog( $original_blog_id ); 

Example:

Here is an example that allows you to use template tags (this works on my multisite install):

// Get current blog
$original_blog_id = get_current_blog_id();

// Setup a category for each blog id you want to loop through - EDIT
$catslug_per_blog_id = array( 
    1 => 'video',
    4 => 'news' 
); 

foreach( $catslug_per_blog_id as $bid => $catslug )
{
    //Switch to the blog with the blog id $bid
    switch_to_blog( $bid ); 

    // Get posts for each blog
    $myposts = get_posts( 
        array( 
            'category_name'  => $catslug,
            'posts_per_page' => 2, 
        )
    );

    // Skip a blog if no posts are found
    if( empty( $myposts ) )
        continue;

    // Loop for each blog
    $li = '';
    global $post;
    foreach( $myposts as $post )
    {
        setup_postdata( $post );
        $li .= the_title(
            $before = sprintf( '<li><a href="https://wordpress.stackexchange.com/questions/98965/%s">', esc_url( get_permalink() ) ),
            $after="</a></li>",
            $echo   = false
        );
    }

    // Print for each blog
    printf(
        '<h2>%s (%s)</h2><ul>%s</ul>',
        esc_html( get_bloginfo( 'name' ) ),
        esc_html( $catslug ),
        $li  
    );
}

// Switch back to the current blog
switch_to_blog( $original_blog_id ); 

wp_reset_postdata();

Here’s a demo screenshot for our above example with site 1 named Beethoven and site 4 named Bach:

demo

PS: Thanks to @brasofilo providing the link that clarifies my misunderstanding of the restore_current_blog() 😉

PPS: Thanks to @ChristineCooper for sharing the following comment:

Just a friendly warning. Make sure to not set your original blog id to
variable $blog_id — this is because during the switch_to_blog()
process, $blog_id will be overriden by the core function, meaning what
when you try to switch back to the original blog, you will end up with
switching to the last one you looped through. A little bit of a
mind-puzzle. 🙂

Leave a Comment