Querying post from a multisite network

You could use your list of blog ids in this way …

$posts = array();
foreach ( $your_list_of_blog_ids as $blog_id ) {
    switch_to_blog( $blog_id );
    $query = new WP_Query(
        array(
            'post_type' => 'any',
            'posts_per_page' => 10,
        )
    );
    while ( $query->have_posts() ) {
        $query->next_post();
        $posts[] = $query->post;
    }
    restore_current_blog();
}

Important are switch_to_blog and restore_current_blog. The rest of the code is just there to illustrate the idea.

Leave a Comment