The MU forums have moved to WordPress.org

restore_current_blog() Not Working as Anticipated (4 posts)

  1. DragonFlyEye
    Member
    Posted 18 years ago #

    Not sure what's going on here, but clearly, restore_current_blog() is not working the way I had intended to use it. Based on what I've read in other functions in wpmu-functions.php, it should be a simple question of using switch_to_blog() to go mess with another blog, then restore_current_blog() should bring you back where you were. Sadly, that's not happening. Here's my code:

    foreach ($blogs as $blog) {
            	if (!in_array($blog['blog_id'], $ignore)) {
            		switch_to_blog($blog['blog_id']);
            		$latest = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_date < NOW() ORDER BY post_date DESC", ARRAY_A);
            		$auth = $latest['post_author'];
            		$latest['post_author'] = $wpdb->get_var("SELECT user_email FROM <code>wp_users</code> WHERE ID = $auth");
            		$words = explode(' ', strip_tags($latest['post_content'], '<br>,<p>'));
            		$tease = '';
            		for($x=0; $x<80; $x++) {
            			$tease .= $words[$x].' ';
            		}
            		$latest['post_content'] = $tease;
            		$latest['post_link'] = get_blog_permalink($blog['blog_id'], $latest['post_id']);
            		if(function_exists(feature_art_gravatar)) { $latest['gravatar'] = feature_art_gravatar($latest['post_author'], 'PG', 80, 'http://dragonflyeye.net/allsorts/default_avatar_48.png'); }
            		$posts[] = $latest;
            	}
            }

    It's definitely switching blogs with switch_to_blog just fine, but the switching back part isn't working. Also, for some reason, the get_blog_permalink function isn't working, either.

    Anybody have any ideas?

  2. andrea_r
    Moderator
    Posted 18 years ago #

    Without looking at the code, restore_current_blog might actually restore the bog as active after a user has marked it deleted and an admin has undeleted it.

  3. DragonFlyEye
    Member
    Posted 18 years ago #

    No, I'm sure of what it's supposed to do. Have a look at this function, directly out of the wpmu-functions.php file:

    function add_blog_option( $id, $key, $value ) {
    	switch_to_blog($id);
    	add_option( $key, $value );
    	restore_current_blog();
    	$opt = $id."-".$key."-blog_option";
    	wp_cache_set($opt, $value, 'site-options');
    }

    It certainly seems as though the point of the function is to first switch to a new blog, perform some task, then switch back to your current blog. Also, you can read the actual function at around line 380.

  4. DragonFlyEye
    Member
    Posted 18 years ago #

    OK, I've figured this out.

    For future reference to those of you who happen upon this question, the problem was not restore_current_blog(), but rather another function I was using, get_blog_permalink(). get_blog_permalink is designed to return the permalink of a post from another blog, thus it switches to that blog momentarily, runs get_permalink() and switches back. Thus it's not appropriate to use when I've already switched to that blog. Hence I got this in pseudocode:

    start on blog 1
    switch to blog 2
       get the latest article
       switch to blog 2
          get the permalink
       restore blog 2 (this is now the "restorable" blog)
    restore blog 2
    end on blog 2

    So, a lesson to anyone trying to use the API's: take care to ensure you use those "remote" blog functions when you're working remotely from blog 1 and the "local" blog functions when you've switched to the blog 2.

About this Topic

  • Started 18 years ago by DragonFlyEye
  • Latest reply from DragonFlyEye