Get post thumbnail url with multisite
If you want to display materials on multisite function without switch_blog_id, then output the image material standard features wordpress fail. For this, I wrote this little function as a turnkey solution is nowhere found.
function multisite_thumb_url($pid, $bid){ //$pid - post id, $bid - blog id
if(isset($pid) && $pid>0){
if(isset($bid) && $bid>0){
global $wpdb;
$curr_blog_id=get_current_blog_id();
$wpdb->set_blog_id( $bid );
$post_thumbnail_id = $wpdb->get_var( "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_thumbnail_id' AND post_id = {$pid}" );
$post_thumbnail_metadata = $post_thumbnail_id ? maybe_unserialize( $wpdb->get_var( "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attachment_metadata' AND post_id = {$post_thumbnail_id}" ) ) : NULL;
if ( ! $post_thumbnail_metadata ){
return false;
}else{
if($bid==1){
$default_post_thumbnail_url = get_blog_option( $bid, 'siteurl' ).'/wp-content/uploads/'.$post_thumbnail_metadata[ 'file' ];
}else{
$default_post_thumbnail_url = get_blog_option( $bid, 'siteurl' ).'/wp-content/uploads/sites/'.$bid."https://wordpress.stackexchange.com/".$post_thumbnail_metadata[ 'file' ];
}
return $default_post_thumbnail_url;
}
$wpdb->set_blog_id( $curr_blog_id );
}else{
return wp_get_attachment_url( get_post_thumbnail_id($pid) );
}
}else{
return wp_get_attachment_url( get_post_thumbnail_id() );
}
}