Posted By

chasecrawford on 06/06/12


Tagged

wordpress multisite


Versions (?)

Recent Posts from All Sites (WordPress Multisite)


 / Published in: PHP
 

A function to query the 10 most recent posts on a multisite WordPress installation

  1. function recent_mu_posts( $howMany = 10 ) {
  2.  
  3. global $wpdb;
  4. global $table_prefix;
  5.  
  6. // get an array of the table names that our posts will be in
  7. // we do this by first getting all of our blog ids and then forming the name of the
  8. // table and putting it into an array
  9. $rows = $wpdb->get_results( "SELECT blog_id from $wpdb->blogs WHERE
  10. public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0';" );
  11.  
  12. if ( $rows ) :
  13.  
  14. $blogPostTableNames = array();
  15. foreach ( $rows as $row ) :
  16.  
  17. $blogPostTableNames[$row->blog_id] = $wpdb->get_blog_prefix( $row->blog_id ) . 'posts';
  18.  
  19. endforeach;
  20. # print_r($blogPostTableNames); # debugging code
  21.  
  22. // now we need to do a query to get all the posts from all our blogs
  23. // with limits applied
  24. if ( count( $blogPostTableNames ) > 0 ) :
  25.  
  26. $query = '';
  27. $i = 0;
  28.  
  29. foreach ( $blogPostTableNames as $blogId => $tableName ) :
  30.  
  31. if ( $i > 0 ) :
  32. $query.= ' UNION ';
  33. endif;
  34.  
  35. $query.= " (SELECT ID, post_date, $blogId as `blog_id` FROM $tableName WHERE post_status = 'publish' AND post_type = 'post')";
  36. $i++;
  37.  
  38. endforeach;
  39.  
  40. $query.= " ORDER BY post_date DESC LIMIT 0,$howMany;";
  41. # echo $query; # debugging code
  42. $rows = $wpdb->get_results( $query );
  43.  
  44. // now we need to get each of our posts into an array and return them
  45. if ( $rows ) :
  46.  
  47. $posts = array();
  48. foreach ( $rows as $row ) :
  49. $posts[] = get_blog_post( $row->blog_id, $row->ID );
  50. endforeach;
  51. # echo "<pre>"; print_r($posts); echo "</pre>"; exit; # debugging code
  52. return $posts;
  53.  
  54. else:
  55.  
  56. return "Error: No Posts found";
  57.  
  58. endif;
  59.  
  60. else:
  61.  
  62. return "Error: Could not find blogs in the database";
  63.  
  64. endif;
  65.  
  66. else:
  67.  
  68. return "Error: Could not find blogs";
  69.  
  70. endif;
  71. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: sb17joker on July 23, 2012

I am building a site & would like the home page of the main site have latest posts from all sites on it. is there a way to use the array/object created with this function to work with a have_posts() loop? I looped it with php to extract the contents in the right places, which I thought would get me wher I wanted to be, but if the post is a gallery, which uses shortcode, it just displays the shortcode, & I would like to display the gallery.

here is what I used...

Posted By: sb17joker on July 23, 2012

well I guess my code didn't go with the comment... it wasn't really important anyways...

Posted By: sb17joker on July 24, 2012

ok, modified the code above to not include the post data on return, just blog_id, timestamp, & ID & used this code below to render it like a normal loop...

    $posts = recent_mu_posts(); 
    $totalposts = count($posts); 
    for($i=0; $i < $totalposts; $i++) : 
                 wp_reset_query();
        switch_to_blog($posts[$i]->blog_id); 
        query_posts('p='.$posts[$i]->ID); 
                    if (have_posts()) : while (have_posts()) : the_post();
Posted By: sortofme on October 8, 2012

Hi.. any way to include the blog_id to the $posts object, so I can retrieve thisn info later? greatly appreciated snippl

Posted By: chasecrawford on November 14, 2012

Sorry for being pretty late, but I found a similar questions that was answered by an accomplished WordPress dev here

The implementation of his solution is a little over my head, but I hope it can lead others in the right direction

Posted By: mhulse on June 6, 2013

Thanks for sharing.

Any easy way to get the permalink?

```

Posted By: mhulse on June 6, 2013

Dangit. Snipplr ate my last post.

Using get_last_updated() and switch_to_blog() will get similar results with potentially less overhead.

Posted By: mhulse on June 6, 2013

Here's a slight variation of the code for anyone wanting it:

Posted By: mhulse on June 6, 2013

Arrrrrgh! It says "Markdown Enabled", but I can't seem to post code blocks!?! Posted here: https://gist.github.com/mhulse/5718743

You need to login to post a comment.