Recent Comments from All Blogs from your Network

Howdy, we’re here again to share a simple snippet of code to create a widget for your WordPress Blog Network. This piece of code will fetch comments from every site and show it as a simple widget on your sidebar. Very useful for Multisite Installations.

Here is the code. Paste it to your functions.php file on your theme folder and you’re all set.


			

    get_results("SELECT blog_id FROM $wpdb->blogs", ARRAY_A); $old = $wpdb->blogid; $comments = array(); foreach ($blogs as $blog) { $wpdb->set_blog_id($blog['blog_id']); $query = get_comments(array('status' => 'approve', 'number' => 3, 'type' => 'comment')); $count = count($query); for ($i = 0;$i < $count;$i++) { $post = get_post($query[$i]->comment_post_ID); $query[$i]->permalink = get_blog_permalink($blog['blog_id'], $post->ID); $query[$i]->post_title = $post->post_title; } $comments = array_merge($comments, $query); } $wpdb->set_blog_id($old); function cmp($a, $b) { if (strtotime($a->comment_date) == strtotime($b->comment_date)) { return 0; } return (strtotime($a->comment_date) < strtotime($b->comment_date)) ? -1 : 1; } usort($comments, "cmp"); $comments = array_slice($comments, 0, $limit); if ($comments) { foreach ($query as $value) { ?>
  • comment_content; ?> on post_title; ?>

    comment_date)); ?>
  • } } else { ?>
  • There are no Comments in this Network

Have a great day 🙂

This entry was posted in Tutorials and tagged , , . Bookmark the permalink.

One Response to Recent Comments from All Blogs from your Network

  1. Benjamin says:

    Great, I’ve been looking for something like this for a long time!
    You forgot a open-php-tag on line 97 and I it only returns one single comment, not the X last. Any idea what that might be?

Leave a Reply

Your email address will not be published. Required fields are marked *