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

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

Read More

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

Related posts

Leave a Reply

2 comments

  1. 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.

  2. switch_to_blog is VERY expensive… here is some code I use (I use my own pager and query vars)

    function recent_mu_posts($howmany, $paged, $when, $until, $search) {
    /*  returns array of posts from all blogs in $blogs, newer than $when
    *   older than $until, containing $search
    */
      global $options;
      global $wp_query;
      global $blogs;
      global $wpdb;
      global $table_prefix;
    
      $blogPostTableNames = get_mu_tables();
        if ( count( $blogPostTableNames ) > 0 ) :
          $query = ''; $i = 0;
          foreach ( $blogPostTableNames as $blogId => $tableName ) :
            if ( $i > 0 ) :
            $query.= ' UNION ';
            endif;
            $query.= " (SELECT ID, post_date, $blogId as `blog_id` FROM $tableName WHERE post_status = 'publish' AND post_type = 'post' AND post_date >= '$when' AND post_date <= '$until'";
            if (isset($search)) $query.= " AND ( post_content COLLATE UTF8_GENERAL_CI LIKE '%{$search}%' OR post_title COLLATE UTF8_GENERAL_CI LIKE '%{$search}% ')";
            $query.=")";
            $i++;
          endforeach;
          $query.= " ORDER BY post_date DESC ";
          /* pagination */
          $totalposts = $wpdb->get_results($query, OBJECT);
          $ppp = intval( get_query_var( 'posts_per_page' ) );
          $on_page = $paged; #intval( get_query_var( 'paged' ) ); 
          if( $on_page == 0 ) $on_page = 1;  
          $offset = ( $on_page - 1 ) * $ppp;
          //set global $wp_query object for pagination
    
          $wp_query->found_posts = count($totalposts);
          $wp_query->max_num_pages = ceil($wp_query->found_posts / $ppp); 
          $wp_query->request = $query . " LIMIT $ppp OFFSET $offset";
    
          //= " LIMIT $ppp OFFSET $offset"; 
          $rows = $wpdb->get_results( $wp_query->request, OBJECT);
          if ( $rows ) :
            $posts = array();
            $i=0;
            global $post;
            foreach ( $rows as $post ) :
                $posts[$i] = get_blog_post( $post->blog_id, $post->ID );
                $posts[$i]->blog_id = $post->blog_id;
                $i++;
            endforeach;
            $wp_query->posts = $posts;
            return $posts;
          endif;
      endif;
    }