You can’t switch in different blogs of the network with the switch_to_blog([1,2]);
function. The function needs a clearly integer value for one blog, see documentation.
WP Core
This process should help you. A custom way with the help of the WP core.
// The sites IDs
$sitesObj = get_sites([
'site__in' => [1, 2,]
]);
// Merge
$sites = object_to_array( $sitesObj );
$args = [
'post_type' => 'post',
'posts_per_page' => 2,
];
// The loop
foreach ($sites as $site) {
switch_to_blog( $site['blog_id'] );
$loop = new WP_Query($args);
if ( $loop->have_posts() ) {
while ( ($loop->have_posts() ) : $loop->the_post();
// content
endwhile;
}
restore_current_blog();
}
Alternative WP Multisite Query
You can also use a custom function, solution, like the solution from Eric – https://github.com/ericandrewlewis/WP_Query_Multisite The solution is well documented.
The way is a little bit smaller, but you need to include this solution as a plugin or inside the Theme.
$query = new WP_Query_Multisite( array( 'post_type' => 'post' ) );
while( $query->have_posts() ) : $query->the_post();
echo $blog_id . get_the_title() . "<br>";
endwhile;
wp_reset_postdata();