La función de WordPress switch_to_blog()
espera un entero como parámetro de entrada. Puede leer más sobre esto en el Codex:
enlace
Por favor, intente este tipo de estructura en su lugar:
// Get the current blog id
$original_blog_id = get_current_blog_id();
// All the blog_id's to loop through
$bids = array( 1, 2 );
foreach( $bids as $bid )
{
// Switch to the blog with the blog_id $bid
switch_to_blog( $bid );
// ... your code for each blog ...
}
// Switch back to the current blog
switch_to_blog( $original_blog_id );
Actualización:
Si desea obtener publicaciones de diferentes categorías para cada blog, puede utilizar, por ejemplo:
// Get current blog
$original_blog_id = get_current_blog_id();
// Setup a category slug for each blog id, you want to loop through - EDIT
$catslug_per_blog_id = array(
1 => 'video',
4 => 'news'
);
foreach( $catslug_per_blog_id as $bid => $catslug )
{
// Switch to the blog with the blog id $bid
switch_to_blog( $bid );
// ... your code for each blog ...
$myposts = get_posts(
array(
'category_name' => $catslug,
'posts_per_page' => 10,
)
);
// ... etc
}
// Switch back to the current blog
switch_to_blog( $original_blog_id );
Ejemplo:
Este es un ejemplo que le permite usar etiquetas de plantilla (esto funciona en mi instalación multisitio):
// Get current blog
$original_blog_id = get_current_blog_id();
// Setup a category for each blog id you want to loop through - EDIT
$catslug_per_blog_id = array(
1 => 'video',
4 => 'news'
);
foreach( $catslug_per_blog_id as $bid => $catslug )
{
//Switch to the blog with the blog id $bid
switch_to_blog( $bid );
// Get posts for each blog
$myposts = get_posts(
array(
'category_name' => $catslug,
'posts_per_page' => 2,
)
);
// Skip a blog if no posts are found
if( empty( $myposts ) )
continue;
// Loop for each blog
$li = '';
global $post;
foreach( $myposts as $post )
{
setup_postdata( $post );
$li .= the_title(
$before = sprintf( '<li><a href="%s">', esc_url( get_permalink() ) ),
$after = '</a></li>',
$echo = false
);
}
// Print for each blog
printf(
'<h2>%s (%s)</h2><ul>%s</ul>',
esc_html( get_bloginfo( 'name' ) ),
esc_html( $catslug ),
$li
);
}
// Switch back to the current blog
switch_to_blog( $original_blog_id );
wp_reset_postdata();
Aquí hay una captura de pantalla de demostración de nuestro ejemplo anterior con el sitio 1 llamado Beethoven y el sitio 4 llamado Bach :

PS:Graciasa@brasofiloqueproporcionaelenlace eso aclara mi interpretación errónea de restore_current_blog()
;-)
PPS: Gracias a @ChristineCooper por compartir el siguiente comentario:
Sólo una advertencia amistosa. Asegúrese de no establecer su ID de blog original en
variable $blog_id
- esto se debe a que durante el switch_to_blog()
proceso, $blog_id
será reemplazado por la función principal, es decir, qué
cuando intentes volver al blog original, terminarás con
cambiando al último por el que pasaste. Un poco de un
rompecabezas mental :)