How do I query all posts of one type across my multisite installation?

Yes but not in a single query, e.g.:

if(is_multisite()){
    global $wpdb;
    $blogs = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public="1""));
    if(!empty($blogs)){
        ?><?php
        foreach($blogs as $blog){
            switch_to_blog($blog->blog_id);
            $details = get_blog_details($blog->blog_id);
            $q = new WP_query();
            if($q->have_posts()){
                while($q->have_posts()){
                    $q->the_post();
                    // do things
                    break;
                }
            }
            wp_reset_query();
            restore_current_blog();
        }
    }
}

If you want to show the latest post in each individual blog that should be easy. If you want to show the individual newest post across the entire network, you’ll need to find the newest of each blog, you will need to store the newest post found, replacing ti when a newer post is found, then outputting after the loop.

Leave a Comment