Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 4 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:

<?php
$original_blog_id = get_current_blog_id(); // get current blog

$bids = array(1,2); // all the blog_id's to loop through
foreach($bids as $bid):
       switch_to_blog($bid); //switched to blog with blog_id $bid
       // ... your code for each blog ...
endforeach ; 

switch_to_blog( $original_blog_id ); //switched back to current blog
?>

Update:

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

<?php
$original_blog_id = get_current_blog_id(); // get current blog

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

$catid_videos = 8; // EDIT
$catid_news = 9; // EDIT

$cats = array(1 => $catid_videos, 2 => $catid_news); // setup a category for each blog

foreach($bids as $bid):
       switch_to_blog($bid); //switched to blog with blog_id $bid

       // ... your code for each blog ...
       $rightbox = get_posts('&cat='.$cats[$bid].'&posts_per_page=10');
       // ... etc

endforeach ; 

switch_to_blog( $original_blog_id ); //switched back to current blog
?>

Example:

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

<h2>News + Videos</h2>
<?php
$original_blog_id = get_current_blog_id(); // get current blog

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

$catid_videos = 3; // EDIT
$catid_news = 2; // EDIT

$cats = array(1 => $catid_videos, 2 => $catid_news); // setup a category for each blog EDIT

foreach($bids as $bid):
        switch_to_blog($bid); //switched to blog with blog_id $bid

        // ... your code for each blog ...
        $posts = get_posts('&cat='.$cats[$bid].'&posts_per_page=10');
        echo "<ul> <strong>Items from blog_id=".$bid."</strong>";
        foreach($posts as $post){
            setup_postdata( $post );

            echo "<li>";
            the_title();
            echo "</li>";

        }
        echo "</ul><hr>";

endforeach ;

switch_to_blog( $original_blog_id ); //switched back to current blog
?>

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

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
    
Check this: restore_current_blog() vs switch_to_blog() –  brasofilo May 10 '13 at 20:22
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
show 12 more comments

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.