Ticket #4297 (closed enhancement: wontfix)

Opened 5 years ago

Last modified 5 years ago

Order Comments

Reported by: hyp0r Owned by: anonymous
Priority: normal Milestone:
Component: General Version:
Severity: minor Keywords: sort comments order by
Cc:

Description

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

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

Change History

hyp0r5 years ago

  • Keywords sort orderby added
  • Type changed from defect to enhancement
  • Keywords comments order by added; orderby removed

comment:4 follow-up: ↓ 5   filosofo5 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.

comment:5 in reply to: ↑ 4   hyp0r5 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.

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;

}

add_filter('comments_array','sort_my_comments');

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

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

Note: See TracTickets for help on using tickets.