The MU forums have moved to WordPress.org

Problem with switch_to_blog() (13 posts)

  1. woocash
    Member
    Posted 14 years ago #

    Could someone please explain why this code always returns name of the first blog (with id=1)?

    $blog_ids = array(1,8);
    
    foreach ($blog_ids as $blog_id) {
    	switch_to_blog($blog_id);
    	$blogname = get_bloginfo('name');
    	echo "blog: ".$blogname."<br />";
    	restore_current_blog();
    }

    And this one f.e. works fine:

    switch_to_blog(1);
    $blogname = get_bloginfo('name');
    echo "blog: ".$blogname."<br />";
    
    switch_to_blog(8);
    $blogname = get_bloginfo('name');
    echo "blog: ".$blogname."<br />";

    And more importantly, could someone help me with fixing the first part? In the end I want to use wp_insert_post in order to post to multiple blogs. For now, all the posts go to blog with id=1.

    Thanks.

  2. tmoorewp
    Member
    Posted 14 years ago #

    Looks like an issue with the foreach loop. Try echoing the $blog_id to see if it changes as you loop through the array.

  3. woocash
    Member
    Posted 14 years ago #

    Yes, it changes.

  4. tmoorewp
    Member
    Posted 14 years ago #

    Try this:

    `foreach ($blog_ids as $blog_id) {
    switch_to_blog($blog_id);
    $blogname = get_bloginfo('name');
    echo "blog: " . $blogname;
    unset($blogname);
    }

    restore_current_blog();`

  5. woocash
    Member
    Posted 14 years ago #

    It doesn't help :/

  6. tmoorewp
    Member
    Posted 14 years ago #

    Ok, so I just wrote a quick plugin that uses this code and it works perfectly fine. The plugin uses a shortcode to print the names of the blogs to a page. Here's the code:

    `function test_blogswitch($attr) {
    global $switched, $wpdb;

    $blog_ids = array(1, 8);

    foreach( $blog_ids as $blog_id ) {
    switch_to_blog($blog_id);
    $blogname = get_bloginfo('name');
    echo "blog: " . $blogname;
    unset($blogname);
    }

    restore_current_blog();
    }

    add_shortcode('do_test_blogswitch', 'test_blogswitch');`

  7. woocash
    Member
    Posted 14 years ago #

    OK, so in your example unset() is the key. How do I manage this then?

    <?php
    require('../wp-blog-header.php');
    $blog_ids = array(1,8);
    
    foreach ($blog_ids as $blog_id) {
    	echo $blog_id; //works fine
    	switch_to_blog($blog_id);
    	// Create post object
    	$my_post = array();
    	$my_post['post_status'] = 'publish';
    	$my_post['post_author'] = 1;
    	$my_post['post_category'] = array(1);
    	$my_post['post_title'] = "Title";
    	$my_post['post_content'] = "Message";
    	// Insert post
    	wp_insert_post($my_post);
    	restore_current_blog();
    }
    ?>

    This is the whole code I have. The problem is that it always inserts into blog with id=1. Am I missing something? Shouldn't it work just fine?

  8. tmoorewp
    Member
    Posted 14 years ago #

    Try adding global $switched, $wpdb; to your code before you declare $blog_ids and after you include the wp-blog-header.php.

  9. woocash
    Member
    Posted 14 years ago #

    I've just tried - still the same.

  10. DeannaS
    Member
    Posted 14 years ago #

    I just tried your exact code - tossed in a plugin to make it display on a page. Works just fine (the very first bit o' code that you said didn't work). I tried it with a non-existent blog id, too (800) and it correctly displays nothing for the second loop. Seems like you might have some sort of funky conflicting something or another going on. I'm assuming this is in a plugin or template file or something - maybe post the rest of the code so we can see if there's something strange? Or, try it on a stock install of WPMU and see if it works there.

  11. woocash
    Member
    Posted 14 years ago #

    Generally, I want to write a small script that will let me post messages to multiple blogs from one webpage. So I would go to a page and have form fields for title, post content and some additional custom stuff for every blog, click submit and have posts submitted to all blogs.

    The code is not within a template and I'm not trying to write a plugin.

    I have only one php file that is located in a folder in main MU WP install directory (mainMU_WP_directory/my_folder/myscript.php). The code in this file is (for now...) exactly as in my 7th post.

    This has been tested and works fine:

    <?php
    require('../wp-blog-header.php');
    // Create post object
    $my_post = array();
    $my_post['post_status'] = 'publish';
    $my_post['post_author'] = 1;
    $my_post['post_category'] = array(1);
    $my_post['post_title'] = "Title";
    $my_post['post_content'] = "Message";
    
    $nr = 1;
    switch_to_blog($nr);
    // Insert post
    wp_insert_post($my_post);
    
    $nr = 8;
    switch_to_blog($nr);
    // Insert post
    wp_insert_post($my_post);
    ?>

    So what is wrong with the loop? I'm confused :/

  12. DeannaS
    Member
    Posted 14 years ago #

    I don't see anything wrong with the loop - that's what I'm saying. It looks like the loop is working just fine and correctly echos the data you'd expect in my install. I don't know why it's not working for you.

  13. woocash
    Member
    Posted 14 years ago #

    Finally, I solved it. The problem was the $blog_id variable. I won't try to explain what exactly was happening as I'm not a php guru. I think it was used by some of the WP functions and it was messing with my code. So the solution was to change the name of $blog_id variable.

    Thank you tmoorewp and DeannaS for the hints :)

About this Topic