Home > categories > Multi site - pass post between sites

Multi site - pass post between sites

May 10Hits:0
Advertisement

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 ?> 

Answers

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() ;-)

Related Articles

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.425 s.