WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

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 ?>
share|improve this question
up vote 8 down vote accepted

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. :)

share|improve this answer
    
here's how i load my posts pastie.org/7827649 how can i implement that into this also like i mention by both blog ids but each blog id by specific category blog 1 will have category videos & the blog 2 will have the category news also get a total posts of 10 limit. – DeadArtcore May 10 '13 at 17:20
1  
I updated the answer to support different categories. – birgire May 10 '13 at 18:39
1  
ps: updated the example again, so you can use template tags like the_title() instead of $post->post_title ;-) I hope I have given you enough info so you can finish your project. – birgire May 11 '13 at 19:08
1  
Replace foreach($rightbox as $post) with foreach($posts as $post) in your pastie. – birgire May 11 '13 at 22:27
1  
Hope it's ok that I added your comment to the updated answer @ChristineCooper – birgire Apr 2 at 22:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.