I have a multisite wordpress installation with 5 sites, and within each site there are the same 4 custom post type,

I want to query the latest 5 posts across all sites where the post_type is gallery

is it possible to get wp_query() to pull across all blogs within the site instead of just the current blog?

2 s
2

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 Reply

Your email address will not be published. Required fields are marked *