1

I'm using the Multisite functionality of WordPress 3.0, and I have a network of sites built where I display one random post from one of the subsites on the main site's front page. I accomplish this, in part, by using the switch_to_blog function from the old WPMU function list.

Up until yesterday I could call a function that called switch_to_blog, created an array, added the blog's name, the post details, the post thumbnail, etc. to the array, and then used restore_current_blog to jump back to the main blog's context. Then I used that array to echo out the things I needed for that post I grabbed. It worked fine.

All of a sudden, when I call switch_to_blog and then from within that block I call bloginfo() just to test it, it still echoes the name of the top level blog, and NOT the switched to blog.

Is that function completely deprecated and unworkable due to random bugs? Anyone have any insight or ideas to get around it, or do I need to write a custom $wpdb->get_results(); query to get around this?

Thanks!

2
  • do you have a caching plugin installed?
    – sorich87
    Commented Nov 29, 2010 at 16:36
  • No caching plugins, no. I'm going to attach my SQL-based workaround solution as an answer. Commented Nov 29, 2010 at 19:13

3 Answers 3

4

It appears that switch_to_blog might be too unpredictable to rely on for major site design. Here's my first attempt at a SQL-based solution.

function get_intro_post($blogid, $thumb_size='') {

  global $wpdb, $post;

  // Get the system defined table prefix
  $prefix = $wpdb->prefix;

  // Create a full table prefix by combining the system prefix with the blogid
  $tbl = $prefix . $blogid;

  // First query selects posts with the 'Introduction' category
  $sql = "SELECT `ID` FROM {$tbl}_posts as p";
  $sql .= " JOIN {$tbl}_term_relationships tr ON p.ID = tr.object_id";
  $sql .= " JOIN {$tbl}_terms t ON tr.term_taxonomy_id = t.term_id";
  $sql .= " WHERE t.name = 'Introduction'";

  // Only want/expect one result row, so use get_row()
  $intro = $wpdb->get_row($sql);
  $postid = $intro->ID;

  // Second query joins the postmeta table to itself so that I can find
  // the row whose post_id matches the _thumbnail_id, found in meta_value, 
  // for the post whose post_id matches the one I found in the previous query.
  $sql = "SELECT p2.meta_key, p2.meta_value FROM  `{$tbl}_postmeta` p1";
  $sql .=   " JOIN  `{$tbl}_postmeta` p2 ON p1.meta_value = p2.post_id";
  $sql .=   " WHERE p1.meta_key =  '_thumbnail_id'";
  $sql .=   " AND p1.post_id =  '{$postid}'";
  $sql .=   " LIMIT 0 , 30";

  // Expecting 2 result rows, so use get_results()
  $thumb_meta = $wpdb->get_results($sql);

  // Set $src to FALSE as the default
  $src = FALSE;

  // Make sure the query returned results
  if(!empty($thumb_meta)) {
    foreach($thumb_meta as $row) {
        // We just want to find the row where meta_key is the attached file
        // and then set our $src var to that value, which is the image path
        if ($row->meta_key == "_wp_attached_file") $src = $row->meta_value;
    }
  }

  // If we found an image path above, I'll append the rest of the path to the front
  if($src) { $src = "/wp-content/blogs.dir/{$blogid}/files/{$src}"; }

  // Handy core function to grab the blogname of another network's blog, by ID
  $blogname = get_blog_details( $blogid )->blogname;

  // Make an associative array holding the elements we need, 
  // some pulled from the global $post context
  $p = array(
    'title' => get_the_title(),
    'content' => get_the_content(),
    'excerpt' => get_the_excerpt(),
    'permalink' => get_permalink(),
    'blogname' => $blogname,
    'thumbnail' => $src
  );

  // Return an object with the post details mentioned above
  return (object) $p;
}
2

There are multiple switch_to_blog() issues related to caching:

Wait until 3.2 for full reliability... (If then...)

0

I'm tempted to aks what happened yesterday. :) New plugins installed? An upgrade?

Yes, the function is deprecated but still in use for plugins, and also quite prone to being unpredictable.

4
  • Good question. The real, honest answer is "absolutely nothing". Commented Nov 29, 2010 at 19:13
  • function isn't deprecated as far as the latest revision. Commented Nov 29, 2010 at 19:35
  • That was what was confusing to me -- but it's definitely unreliable in some way, at least it seems to have been for me. Commented Nov 29, 2010 at 20:48
  • If not officially deprecate,d it will be and is also not advised unless you're aware of how it works and its limitations.
    – andrea_r
    Commented Dec 3, 2010 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.