Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. function txx_top_posts_mu( $howMany = 10 ) {
  2. global $wpdb;
  3. global $table_prefix;
  4.  
  5. // get an array of the table names that our posts will be in
  6. // we do this by first getting all of our blog ids and then forming the name of the
  7. // table and putting it into an array
  8. $rows = $wpdb->get_results( "SELECT blog_id from $wpdb->blogs WHERE
  9. public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0';" );
  10. if ( $rows ) :
  11. $blogPostTableNames = array();
  12. foreach ( $rows as $row ) :
  13. $blogPostTableNames[$row->blog_id] = $wpdb->get_blog_prefix( $row->blog_id ) . 'posts';
  14. endforeach;
  15. //print_r($blogPostTableNames);
  16.  
  17. // now we need to do a query to get all the posts from all our blogs
  18. // ordered by the number of comments and with limits applied
  19. if ( count( $blogPostTableNames ) > 0 ) :
  20. $query = '';
  21. $i = 0;
  22. foreach ( $blogPostTableNames as $blogId => $tableName ) :
  23. if ( $i > 0 ) :
  24. $query.= ' UNION ';
  25. endif;
  26. $query.= " SELECT ID, comment_count, $blogId as `blog_id` FROM $tableName ";
  27. $i++;
  28. endforeach;
  29. $query.= " ORDER BY comment_count DESC LIMIT 0,$howMany;";
  30. //echo $query;
  31. $rows = $wpdb->get_results( $query );
  32.  
  33. // now we need to get each of our posts into an array and return them
  34. if ( $rows ) :
  35. $posts = array();
  36. foreach ( $rows as $row ) :
  37. $posts[] = get_blog_post( $row->blog_id, $row->ID );
  38. endforeach;
  39. //print_r($posts);
  40. return $posts;
  41. endif;
  42. endif;
  43. endif;
  44. return false;
  45. }