WP MS: How to query over the network

[*]

Each site in a Multisite network is considered to be exactly that — a separate site. All the $wpdb values (eg, $wpdb->posts) are distinct for each site.

You can use switch_to_blog() and restore_current_blog() to get content (posts, etc) from the various sites[*] in your network:

$sites = array( 1, 2, 3, );
$all_posts = array();
$args = array( 'category_name' => 'xxx', );
foreach( $sites as $site ) {
    switch_to_blog( $site );
    $posts = get_posts( $args );
    foreach( $posts as $post ) {
        // Better get the post's permalink, since it's site-dependent
        $post->permalink = get_permalink( $post->ID );
        $all_posts[] = $post;
    }
    restore_current_blog();
}

This will get you an array of all the posts from each of your sites — each one will appear in the array as a WP_Post object. To sort them all by date, you can use PHP’s usort() function and compare the $post->post_date or $post->post_date_gmt values.

References


[*]: Initially WordPress used the terminology “a site of blogs“. Later it was refined to “a network of sites“, but some of the function names still reflect the old ways.

[*]