Categorized | Tips, Tutorials, WordPress

How To: Rearrange WordPress Comments (Latest On Top)

A few days ago i had to make a minor change to my friend’s site, he wanted the latest comments to be right on top instead of the usual at the bottom of a page, so I had to re-arrange how the comments appeared and if for some reason you need this function all you have to do is a minor edit of /wp-includes/comment-template.php.

Original Code:
// TODO: Use API instead of SELECTs.
if ( $user_ID) {
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND (comment_approved = '1' OR ( user_id = '$user_ID' AND comment_approved = '0' ) ) ORDER BY comment_date");
} else if ( empty($comment_author) ) {
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND comment_approved = '1' ORDER BY comment_date");
} else {
$author_db = $wpdb->escape($comment_author);
$email_db = $wpdb->escape($comment_author_email);
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND ( comment_approved = '1' OR ( comment_author = '$author_db' AND comment_author_email = '$email_db' AND comment_approved = '0' ) ) ORDER BY comment_date");
}

Amazingly enough all you have to do is add a four letter word… DESC :

// TODO: Use API instead of SELECTs.
if ( empty($comment_author) ) {
$comments = $wpdb->get_results(“SELECT * FROM $wpdb->comments WHERE comment_post_ID = ‘$post->ID’ AND comment_approved = ‘1′ ORDER BY comment_date DESC“);
} else {
$author_db = $wpdb->escape($comment_author);
$email_db = $wpdb->escape($comment_author_email);
$comments = $wpdb->get_results(“SELECT * FROM $wpdb->comments WHERE comment_post_ID = ‘$post->ID’ AND ( comment_approved = ‘1′ OR ( comment_author = ‘$author_db’ AND comment_author_email = ‘$email_db’ AND comment_approved = ‘0′ ) ) ORDER BY comment_date DESC“);
}

That’s it!

UPDATE: I fired up my Dreamwever and can confirm that you’ll find the code in just about line 290

If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts:

  1. WordPress Plugin – Sexy Comments
  2. Wordpress Plugin – Genki YouTube Comments
  3. Reply and Manage your Comments a little better with the Absolute Comments Plugin
  4. Tips for reducing spam comments in Wordpress
  5. The importance of comments in Wordpress

23 Comments For This Post

  1. Calaelen Says:

    Remember this change bevor you do a wordpress update, because it is a system file which usualy gets overwritten in a full update.

  2. NBWeb.it Says:

    Hello,

    i have tried to apply it but it does not work on my existing posts… i am using Wp 2.3.3…

  3. Sudar Says:

    If you don’t want to edit the core files, then you can use my Plugin to do it. http://sudarmuthu.com/wordpress/reverse-comments

    All you have to do is to drop my Plugin the Plugin directory and then activate it, you don’t need to edit the theme or any of the core files.

  4. poer Says:

    hi moses, you can get the same result by using array_reverse php function to the $comments array before comment loop in comments.php file.

  5. ugur Says:

    thanks I working http://www.ucretsiztarot.com or http://www.kompozisyonornekleri.com my web site .Thakns :)

  6. catering Says:

    very nice blog :)

  7. doni Says:

    Advertising dikantorku lagi ada lowongn nich untuk staf kantor ada yang minat?lumayan lo salerinya berkisar 800-1 juta, bisa sambil kuliah pula, yang minat sms ya…
    di bandung

    (HRD : Achmad setiady (022-93418777)

  8. Godserv Says:

    This is a good way to accomplish the task. I usually us wp ajaxed plugin to accomplish this task but, I will use this now since I will cut down on the load time without that plugin :) Thanks a bunch.

  9. Lonnie Singleton Says:

    qldl3zk9i5e52z45

  10. Sajid Says:

    This is good way to complete our task easily…
    Thanks for it but at some point it conflict otherwise good try…

  11. Lakshmi Mareddy Says:

    I bumped into your site while looking for something else. Just wanted to point out what I did with reverse order in comments(http://www.chilligavva.com/2008/01/17/order-in-comments.html). I would be interested in figuring out how to paginate comments, for a said post/page.

    Rgds..

  12. catering Says:

    wery nice blog :) thank you admin

  13. shoaib hussain Says:

    works like a charm.thnx a lot

  14. Infographiste Says:

    Hreat job! thanks mate for your kind assistance :)

  15. Rosario Says:

    i have tried to apply it Thanks :)

  16. Joltivan Says:

    Thanks for the code :D

  17. kelly Says:

    What if my code is
    ORDER BY comment_date_gmt”, $post->ID, $user_ID));
    instead?

    How can it be changed?

    Thanks in advance!

  18. Rob Johnson Says:

    Currently the chunk of code is on line 844 in Wordpress 2.8.4
    Thank you, great tip!

  19. catering Says:

    wery nice blog

  20. Trackback - Cheap Internation Call >> How to make cheap international call Says:

    ,..] http://www.wpthemesplugin.com is other must read source on this issue,..]

  21. Steve Call Says:

    Thanx for this well written article. But I had difficulty navigating through your website because I kept getting 502 bad gateway error. Just thought to let you know.

  22. Chicago Website Company Says:

    Thanks a lot. I’ve been searching google for the answer and this is exactly what i was looking for. Worked like a charm.

  23. scarlet Says:

    is there a way to bring up the post which has the latest comment on top? like in forums? plz advice..

15 Trackbacks For This Post

  1. How To: Displays Most Recent Comment First Says:

    [...] By default, a typical WordPress blog will display the very first comment at the top of the comments template.  While I prefer this way, many others do not.  If you are someone that would like your WordPress blog to display the most recent comment on top, Moses of WPThemesPlugin.com explains how to rearrange your WordPress comments. [...]

  2. Cambiare l’ordine dei commenti in Wordpress Says:

    [...] basta leggere questo tutorial di [...]

  3. Latest Comment On Top | WordPress [at] mimpikami . com Says:

    [...] artikel dengan judul How To: Rearrange WordPress Comments (Latest On Top) ini, Moses dari wpthemesplugin memberikan cara dengan sedikit mengubah file [...]

  4. Top 10 Tutorials for Working with WordPress Comments Says:

    [...] How to: Rearrange WordPress Comments (Latest on Top) [...]

  5. Geek Week - This week jQuery tutorials, CSS3, & WordPress - PHP, XHTML, Web Design, Flash | JasonCypret.com Says:

    [...] Rearrange WordPress Comments Latest On Top [...]

  6. 30 Most Wanted WordPress Comments Page Hacks | instantShift Says:

    [...] Click Here for More [...]

  7. 30 astuces pour améliorer la présentation des commentaires dans WordPress | Presse-Citron Says:

    [...] 06. Affichez le dernier commentaire en premier [...]

  8. WordPress Comments Guide | WordPress Comment Hacks & Plugins Says:

    [...] the Conversation Going A great way to keep the conversation going is to display the most recent comments first.  This way the user doesn’t have to scroll down to the bottom of a long list to see if there [...]

  9. 30个Wordpress 评论回复功能扩展技巧 - 菠菜博 Says:

    [...] 6.把最早的回复显示在最前面 [...]

  10. 30个增强Wordpress留言部份的技巧 at 毅人博客空间 Says:

    [...] 6.把最早的回复显示在最前面 [...]

  11. 30个增强Wordpress留言部份的技巧 - 左岸读书_blog Says:

    [...] 6.把最早的回复显示在最前面 [...]

  12. Wordpress plugin and hacks V | Beat Fly Blog Says:

    [...] Displaying The Most Recent Comment First, By default, a typical WordPress blog will display the very first comment at the top of the comments template. But if you would like your WordPress blog to display the most recent comment to be right on top instead of the usual at the bottom of a page, Moses of WPThemesPlugin.com suggested a tricky solution to display recent comments on top. [...]

  13. WordPress资料站 » Blog Archive » 更为友好的Wordpress回复页技巧 Says:

    [...] 6.把最早的回复显示在最前面 [...]

  14. 30个你最想要的Wordpress回复页技巧 - 落落寡欢 Says:

    [...] 6.把最早的回复显示在最前面 [...]

  15. Advanced Wordpress Comment Styles and Tricks | Pro Blog Design Says:

    [...] How to Rearrange WordPress Comments: Latest on Top [...]

Leave a Reply

Categories

-->