WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

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

    global $wpdb;
    $blogs = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public='1'"));
        foreach($blogs as $blog){
            $details = get_blog_details($blog->blog_id);
            $q = new WP_query();
                    // do things

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.

share|improve this answer

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 ';
        $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.= " 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();
        global $post;
        foreach ( $rows as $post ) :
            $posts[$i] = get_blog_post( $post->blog_id, $post->ID );
            $posts[$i]->blog_id = $post->blog_id;
        $wp_query->posts = $posts;
        return $posts;
share|improve this answer
Can you provide your code for get_mu_tables()? I'm assuming that's a custom function you created? I found this snippet which appears to be from your code with that addition - snipplr.com/view/65413 – James Nov 14 '13 at 19:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.