Make WordPress Core

Opened 17 years ago

Closed 17 years ago

#4297 closed enhancement (wontfix)

Order Comments

Reported by: hyp0r's profile hyp0r Owned by:
Milestone: Priority: normal
Severity: minor Version:
Component: General Keywords: sort comments order by
Focuses: Cc:


Allow comments to by sorted in descending order.

On some pages on my site I like to list comments with the newest comment on top. Every time that I upgrade I have to make these changes to the comment-template.php

line 280:
add , $order = 'ASC' to the comments_template function:

function comments_template( $file = '/comments.php', $order = 'ASC' ) {

line 292, 294 & 298:
add $order after ORDER BY comment_date

$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 $order");

With the above modifications the code will continue to work exactly the way it does now. But you can use this code:

<?php comments_template('/comments.php','DESC'); ?>

and it will display the comments in descending order.

I don't see any reason that this would cause any problems. I'd love to help add this feature (it's quite simple). I'm not sure how to do that, if someone could point me in the right direction I'd love to help out.

Attachments (1)

comment-template.php (10.6 KB) - added by hyp0r 17 years ago.

Download all attachments as: .zip

Change History (8)

#1 @hyp0r
17 years ago

  • Keywords sort orderby added

#2 @hyp0r
17 years ago

  • Type changed from defect to enhancement

#3 @hyp0r
17 years ago

  • Keywords comments order by added; orderby removed

#4 follow-up: @filosofo
17 years ago

The comments get passed through the 'comments_array' filter, so you should be able to use a callback function to re-sort them then.

#5 in reply to: ↑ 4 @hyp0r
17 years ago

Replying to filosofo:

The comments get passed through the 'comments_array' filter, so you should be able to use a callback function to re-sort them then.

Can you please explain the 'comments_array' filter to me? I'm not familiar with how that works.

#6 @filosofo
17 years ago

You can read about plugin filters here.

Before the comments_template function prints the comments template, it filters the comments through the comments_array filter.

function sort_my_comments($comments) {

[do sorting]

return $comments;



#7 @rob1n
17 years ago

  • Milestone 2.4 deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Many people do this already with a plugin and add_filter().

Note: See TracTickets for help on using tickets.