Recent Comments from All Blogs from your Network

By Steven Chang

December 13, 2010


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 🙂

Steven Chang

About the author

The roles I play: Leader, Husband, Dad, Son, Brother, Friend, Programmer, Investor, Trader, Marketer, Student, Teacher, Influencer.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Direct Your Visitors to a Clear Action at the Bottom of the Page

>