Codeable info

Get Comments From Across A Multi Site Network

Posted on by in WordPress Tutorials

If you run a network of WordPress sites on the WordPress 3.0+ multi site mode, then you may want the ability to create a “network chatter” box of comments from across the network. While this is not the simplest task, it’s not too difficult if you know how. After a lot of tweaking, I’ve managed to put together a function that will do just that. So here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function get_network_comments()
{
	global $wpdb;
	$number = 8; // maximum number of comments to display
	$selects = array();
	foreach (wp_get_sites($args) as $blog)
		// select only the fields you need here!
		$selects[] = "(SELECT comment_post_ID, comment_author, comment_author_email, comment_date_gmt, comment_content, post_title, {$blog['blog_id']} as blog_id FROM {$wpdb->base_prefix}{$blog['blog_id']}_comments
		  LEFT JOIN {$wpdb->base_prefix}{$blog['blog_id']}_posts
		  ON comment_post_id = id
		  WHERE post_status = 'publish'
			AND post_password = ''
			AND comment_approved = '1'
			AND comment_type = ''
		   ORDER BY comment_date_gmt DESC LIMIT {$number})"; // real number is (number * # of blogs)
 
		$comments = $wpdb->get_results(implode(" UNION ALL ", $selects)." ORDER BY comment_date_gmt DESC", OBJECT);
 
		$count = 0;
		foreach((array)$comments as $comment):
		  $count++;
		  if($count == $number+1) break; 
		  ?>
		  <li>
			    comment_author_email, 32); ?>
				<span class="chatter-author">comment_author; ?></span> on 
			    <a class="blog-link" href="blog_id, $comment->comment_post_ID); ?>" title="commented on post_title); ?>">
				comment_content); ?>
 
		  </li>
		<?php
	endforeach;
}

Then, to display the comments in an unordered list, you’d do this:

1
2
3
<ul>
	<?php get_network_comments(); ?>
</ul>

Enjoy!

Pippin

)
Codeable info

Comments (7)

Comment by John says:

Wow! This is exactly what I have been looking for!!! Just one question: Is it possible to edit this code so it get posts from ONE defined category? And by that, duplicate the code and call the function something else and use it to get posts from anotehr defined category and display both on one page? (for my use, this woulfd have been just perfect for my “mother page” on my multisite install.
Do you think this is possible?

Comment by Sagive says:

hey pipin – thanks for sharing but for the best of my knowledgh there is no wp_get_sites in wordpress… is this a function you created?

Comment by Pippin says:

Ok, no, this does not allow you to do that.

Comment by Bob Richards says:

I did not explain well. I am interested in moderating all comments from a central place. I have 200 sites in a multi-site installation and my goal is to avoid having to access each dashboard for each site and simply have all comments to be moderated from all sites aggregated centrally.

Comment by Pippin says:

This code allows you to displays comments from across the network on one single site. It allows you to display them to the public, much like the default ‘latest Comments” widget, but pulls from all sites.

Comment by Bob Richards says:

Can you clarify
I have a mutlisite install with 200 sub-sites. I would like to see all of the comments ion one place rather than need to enter the dashboard of each site. is that what this hack doers?

Comment by sambaker67 says:

This NEW program allows homeowners to walk away from their underwater mortgage with absolutely no penalty from their mortgage lender and get $3000.

Codeable info