The MU forums have moved to WordPress.org

switch_to_blog wackiness, again? (14 posts)

  1. DragonFlyEye
    Member
    Posted 15 years ago #

    We discussed this in this thread, but I'm starting a new one for clarification.

    This seems to be a problem with setting $wpdb variables when using switch_to_blog() on WPMU2.6. Example output (the base code worked on every previous iteration of WPMU:

    $blogs = get_last_updated();
    foreach ($blogs as $next_blog) {
    	switch_to_blog($next_blog['blog_id']);
    	$latest->blog_link = get_bloginfo('url');
    	$latest->blog_name = get_bloginfo('name');
    	/* a bunch of other commented out stuff */
    	$posts[] = $latest;
    	restore_current_blog();
    }

    If you change $posts[] = $latest to $posts = $blog_id, you get:

    Array
    (
        [0] => 1
        [1] => 11
        [2] => 4
        [3] => 6
        [4] => 5
    )

    But if you just echo what its supposed to output, you get:

    Array
    (
        [0] => stdClass Object
            (
                [blog_link] => http://holisticnetworking.net/design
                [blog_name] => The HN Design Studio
            )
    
        [1] => stdClass Object
            (
                [blog_link] => http://holisticnetworking.net/design
                [blog_name] => The HN Design Studio
            )
    
        [2] => stdClass Object
            (
                [blog_link] => http://holisticnetworking.net/design
                [blog_name] => The HN Design Studio
            )
    
        [3] => stdClass Object
            (
                [blog_link] => http://holisticnetworking.net/design
                [blog_name] => The HN Design Studio
            )
    
        [4] => stdClass Object
            (
                [blog_link] => http://holisticnetworking.net/design
                [blog_name] => The HN Design Studio
            )
    
    )

    So, switch_to_blog appears to not be switching up the necessary variables in $wpdb, unless I've missed something?

  2. donncha
    Key Master
    Posted 15 years ago #

    Are you using an object-cache? I tried the code above and it worked fine on 2.6.1-beta1, but the switch_to_blog() code hasn't changed since 2.6 AFAIR.

  3. konus
    Member
    Posted 15 years ago #

    I know that switch_to_blog it's a little buggy right now. I'm solve some issues doing in a different way: passing the blog_id, always when you can. For example, in your code you can use:


    $latest->blog_link = get_blog_option($next_blog['blog_id'], 'siteurl' );

    *This is not a hack*, it's just another way (more long) to do "the same". In wpmu 2.6-1-beta1 the switch_to_blog function was working great, but in the release, not all works... I'm wondering why...

  4. MrBrian
    Member
    Posted 15 years ago #

    Just so you are aware, get_blog_option uses switch_to_blog, so there isn't a difference :P.

  5. konus
    Member
    Posted 15 years ago #

    Yes, maybe not a code level, but in practice yes. If you try:

    switch_to_blog($blog_id);
    $blogname = get_bloginfo('name');
    $thistemplateurl = get_template_directory_uri();
    $thisthetime = get_post_time("M j, Y");
    restore_current_blog();

    the var "thisthetime" get the value from the original call ($current_blog_id) and not the $bog_id

    ... and I feel sorry for that :-P

  6. MrBrian
    Member
    Posted 15 years ago #

    Why would switching to a different blog also switch the $post global? switch_to_blog is meant to change the blog_id, not post_id. get_post_time is a function used in themes and relies on the loop, so really it has nothing to do with switch_to_blog.

  7. DragonFlyEye
    Member
    Posted 15 years ago #

    Are you using an object-cache? I tried the code above and it worked fine on 2.6.1-beta1, but the switch_to_blog() code hasn't changed since 2.6 AFAIR.

    Yes, I know it works on the beta, because I remember you patching it. I'm not sure why it's different now. I am not using any kind of caching for this site at the moment.

    Sorry, it's been a while since I replied back to this.

  8. DragonFlyEye
    Member
    Posted 15 years ago #

    FYI, I tried swapping to use the "get_option()" function and it didn't change anything. Not that I really thought it would, I'm just trying things to see what happens:

    $latest->blog_link = get_option('siteurl');
    $latest->blog_name = get_option('blogname');

  9. DragonFlyEye
    Member
    Posted 15 years ago #

    Sorry for the multi-post, but I also tried wp_cache_flush() to see what would happen, no change.

    switch_to_blog($next_blog['blog_id']);
    wp_cache_flush();
  10. MrBrian
    Member
    Posted 15 years ago #

    Have you tried a whole new install DragonFlyEye?

  11. DragonFlyEye
    Member
    Posted 15 years ago #

    No, but I do suppose that might be an option.

  12. DragonFlyEye
    Member
    Posted 15 years ago #

    OK, I deleted EVERYTHING off the server and installed a fresh copy of WordPress MU 2.6.1. I'm still having the same problem with switch_to_blog.

    Is there seriously no one else having this problem? I don't know what else I can do to resolve this issue.

  13. konus
    Member
    Posted 15 years ago #

    how about using 'get_blog_option' instead of 'get_option'?

  14. donncha
    Key Master
    Posted 15 years ago #

    DragonFlyEye - it still works fine on my test server, and yes, if you're trying to interrogate the current post, you need to use get_post() or something after switching blog.

About this Topic

  • Started 15 years ago by DragonFlyEye
  • Latest reply from donncha