WordPress Recent Comments Hack

I wanted to get the most recent comments in WordPress without having to use the widgets or any plugins. When you use any of the widgets it will override the sidebar.php (although you could put what you needed in a else bracket inside sidebar.php, but I figured I’d put this hack together anyway).

This hack would go in your sidebar.php

$comment_array = $wpdb->get_results("SELECT comment_date_gmt, comment_author, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 10");
$comment_total = count($comment_array);
echo '<ul>';
for ($x = 0; $x < $comment_total; $x++)
echo '<li>';
echo $comment_array[$x]->comment_author . ' on ';
echo '<a href="'. get_permalink($comment_array[$x]->comment_post_ID) . '#comment-' . $comment_array[$x]->comment_ID . '">';
echo get_the_title($comment_array[$x]->comment_post_ID);
echo '</a>';
echo '</li>';
echo '</ul>'

Tags: ,

Comments 1-4 of 4 to “WordPress Recent Comments Hack”

  1. 1 · Datzerox says:

    I was looking for this for a long time, Thanks you very much!

  2. 2 · Davey says:

    Excellent! This is exactly what I was wanting to do. Thanks for posting it up

  3. 3 · Anonymous says:

    Thanks, it works

  4. 4 · Book of Ra austricksen says:

    Im Grunde genommen n guter Kommentar, aber kannst du spater nicht n wenig ausfuhrlicher schreiben? 😉

Leave a Comment

Comments are reviewed before publishing to prevent spam.