WordPress Multi-Site: Get Featured Image from Another Blog

Yesterday, David Bisset posted a tweet asking the following question:

Is there a function similar to get_the_post_thumbnail for multi-site (to grab a featured image from a post of a particular blog id)?

At the time, it seemed that the best way to do so was to use the Sitewide Tags plugin for WordPress, which, I believe, does offer some functionality similar to this (I haven’t used the plugin, yet, so I can’t say for sure).

However, I took the liberty of using some code I posted here on HTMLCenter a few weeks ago as a starting point, then writing up some functions to pull featured images from other blogs in the network. The functions below are similar to the get_the_post_thumbnail(), the_post_thumbnail() and has_post_thumbnail() functions found in the WordPress core, except that they each take one additional parameter: the ID of the blog from which you want to pull the thumbnail.

The code below has only been minimally tested, so there may be issues or bugs in it. If you find any issues or bugs, please let me know.

Note: Please ignore the code immediately below. An update has been added to the bottom of this post.

if( !function_exists( 'get_the_post_thumbnail_by_blog' ) ) {
	function get_the_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL,$size='post-thumbnail',$attrs=NULL) {
		global $current_blog;
		$sameblog = false;

		if( empty( $blog_id ) ) {
			$blog_id = $current_blog->ID;
			$sameblog = true;
		}
		if( empty( $post_id ) ) {
			global $post;
			$post_id = $post->ID;
		}
		if( $sameblog )
			return get_the_post_thumbnail( $post_id, $size, $attrs );

		if( !has_post_thumbnail_by_blog($blog_id,$post_id) )
			return false;

		global $wpdb;
		$wpdbobj = clone $wpdb;
		$wpdb->flush();
		$wpdb->blogid = $blog_id;
		$wpdb->set_prefix( $wpdb->base_prefix );

		$blogdetails = get_blog_details( $blog_id );
		return str_replace( $current_blog->domain . $current_blog->path, $blogdetails->domain . $blogdetails->path, get_the_post_thumbnail( $post_id, $size, $attrs ) );

	}

	function has_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL) {
		if( empty( $blog_id ) ) {
			global $current_blog;
			$blog_id = $current_blog;
		}
		if( empty( $post_id ) ) {
			global $post;
			$post_id = $post->ID;
		}

		global $wpdb;
		$wpdbobj = clone $wpdb;
		$wpdb->flush();
		$wpdb->blogid = $blog_id;
		$wpdb->set_prefix( $wpdb->base_prefix );

		$thumbid = get_post_meta( $post_id, '_thumbnail_id', true );
		$wpdb = clone $wpdbobj;
		return ($thumbid !== false) ? true : false;
	}

	function the_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL,$size='post-thumbnail',$attrs=NULL) {
		echo get_the_post_thumbnail_by_blog($blog_id,$post_id,$size,$attrs);
	}
}

Update

After speaking with Andrea_R on Twitter about this, I found that there is a much better way to perform these actions. She first recommended simply querying the database directly. That’s still entirely possible (especially if we find that the code below doesn’t scale very well for some reason), but the main reason I am using the native get_post_thumbnail() function rather than rewriting the <img> code for the thumbnail within my function is that it will (hopefully) be somewhat future-proof. If the WordPress devs decide to update or change the HTML code that the get_post_thumbnail() function outputs, the functions below will be automatically updated by proxy.

Regardless, she pointed out a method of the WPDB class of which I was previously unaware. The method is called get_blog_prefix, and, while my code below doesn’t call that function itself, it did turn me onto the “set_blog_id” method within the WPDB class.

Therefore, rather than having to clone the WPDB object, make changes to it, then restore it, the modified code simply calls the set_blog_id() method to make the necessary changes. Hopefully this code will scale, but I’ll really only know that once we do some major testing with it.

As I mentioned above, if you have any further suggestions for improvement or optimization, please feel free to share them.

if( !function_exists( 'get_the_post_thumbnail_by_blog' ) ) {
	function get_the_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL,$size='post-thumbnail',$attrs=NULL) {
		global $current_blog;
		$sameblog = false;

		if( empty( $blog_id ) || $blog_id == $current_blog->ID ) {
			$blog_id = $current_blog->ID;
			$sameblog = true;
		}
		if( empty( $post_id ) ) {
			global $post;
			$post_id = $post->ID;
		}
		if( $sameblog )
			return get_the_post_thumbnail( $post_id, $size, $attrs );

		if( !has_post_thumbnail_by_blog($blog_id,$post_id) )
			return false;

		global $wpdb;
		$oldblog = $wpdb->set_blog_id( $blog_id );

		$blogdetails = get_blog_details( $blog_id );
		$thumbcode = str_replace( $current_blog->domain . $current_blog->path, $blogdetails->domain . $blogdetails->path, get_the_post_thumbnail( $post_id, $size, $attrs ) );

		$wpdb->set_blog_id( $oldblog );
		return $thumbcode;
	}

	function has_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL) {
		if( empty( $blog_id ) ) {
			global $current_blog;
			$blog_id = $current_blog;
		}
		if( empty( $post_id ) ) {
			global $post;
			$post_id = $post->ID;
		}

		global $wpdb;
		$oldblog = $wpdb->set_blog_id( $blog_id );

		$thumbid = has_post_thumbnail( $post_id );
		$wpdb->set_blog_id( $oldblog );
		return ($thumbid !== false) ? true : false;
	}

	function the_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL,$size='post-thumbnail',$attrs=NULL) {
		echo get_the_post_thumbnail_by_blog($blog_id,$post_id,$size,$attrs);
	}
}

Also, I thought it might be a good idea to mention; if you decide to try querying the database directly rather than using the native WordPress functions for retrieving a thumbnail, the place you’ll want to start is within the postmeta table, and the meta key for the item is _thumbnail_id. Once you retrieve that _thumbnail_id from the database, you should be able to retrieve the necessary information about that thumbnail from the posts and postmeta tables in the database.

Did you like this post? Get monthly summary of our new tutorials, posts and tips to your inbox!


Developer Resources