Get posts from sites in Multisite?

I’m trying to pull multiple sites posts. For example, I can pull out a single site
posts by a category and total posts 10.

But I’m trying to pull out both posts from two separate Multisite blogs 1 & 2. But only blog 1 works. Also, I want to pull out another category from blog 1 and blog 2 by another category. How can I achieve this?

Read More

Here’s what I am trying to do:

<?php
global $switched;
switch_to_blog(1,2); //switched to 1 & 2 but only 1 working

// Get latest Post
$latest_posts = get_posts('&cat=64&showposts=10');
$cnt =0;?> 
    <ul>
    <?php foreach($latest_posts as $post) : setup_postdata($post);?>
    <li>
        <a href="<?php echo get_page_link($post->ID); ?>" title="<?php echo $post->post_title; ?>"><?php echo  short_title('...', 7); ?></a>
    </li>                                
<?php endforeach ; ?>

<?php restore_current_blog(); //switched back to main site ?>

Related posts

2 comments

  1. 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="%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. 🙂

  2. Take a look at the code in my “Multisite Post Reader” plugin https://wordpress.org/plugins/multisite-post-reader/ . It uses the technique in the other answer to loop through the posts. I also have plugins that do the same thing for images.

    Since it is an open-source code, you are welcome to wander through the code and use pieces of it for your own use. (Some of the code is modified from open-source code I found.)

Comments are closed.