The MU forums have moved to WordPress.org

switch_to_blog problem (3 posts)

  1. xyzzy
    Member
    Posted 14 years ago #

    Hi, I am having a problem with switch_to_blog and restore_current_blog.

    WPMU switches to the new blog, but doesn't seem to switch back properly. My code is below. I'd like to switch to a blog, gets some posts, and switch back. The final the_content in the code should show content from the original blog's posts, but it shows the last entry gotten from the blog that's been switched to.
    commenting out setup_postdata($post); seems to help a little. the_content works, but the_title does not.

    I'm perplexed. Thanks for any help.

    --

    global $switched;
        switch_to_blog(15);
            $lastposts = get_posts('numberposts=3');
            foreach($lastposts as $post) :
                    setup_postdata($post);
                    the_excerpt();
                    get_the_image();
            endforeach;
        restore_current_blog();  
    
    the_content(); // this should print the_content from the original blog page/post
  2. tmoorewp
    Member
    Posted 14 years ago #

    You need to call get_posts again after you restore_current_blog(). get_posts creates a cache of the last call it had.

    Also, what is global $switched supposed to do?

  3. xyzzy
    Member
    Posted 14 years ago #

    Thanks. I copied global $switched from the sample code here:

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

    I appreciate your help. I'll take a closer look. Thanks.

About this Topic