0 down vote favorite

I have a wordpress single post where I have comment system - Live Site

And I have two links one is: 1. newest 2. oldest

The thing I want is when I will click on newest it will display the comments list in newest order and if I click on oldest it will display the comments list from oldest to newest.

Any simple solution is fine (though I prefer using ajax but no need for ajax right now). GET and POST method will be fine.

Any clue?

Thanks in adavance


Just open the comments.php file from your theme. Find the following line (or similar)

<?php foreach ($comments as $comment) : ?>

Add this lines just above it

if (isset($_GET['comOrder']) && $_GET['comOrder'] == 'Newest' )
    $comments = array_reverse($comments, true);

so your code looks like this:

<?php if (isset($_GET['comOrder']) && $_GET['comOrder'] == 'Newest' )
        $comments = array_reverse($comments, true);
foreach ($comments as $comment) : ?>

and then make sure your newest first like points to your current location with ?comOrder=Newest


In case you are using wp_list_comments then simply add reverse_top_level parameter set to true when newest first link is clicked so something like this should work:

$args = array('type' =>'comments','callback' => 'mytheme_comment');
if (isset($_GET['comOrder']) && $_GET['comOrder'] == 'Newest' )
    $args['reverse_top_level'] = true;
  • well, thanks for that, but i dont have foreach, i have wp_list_comments('type=comments&callback=mytheme_comment'). mytheme_comment is my customised function in functions.php file. So should i put your code before that wp_list_comments function? – Zakir Sajib Aug 19 '12 at 13:18
  • updated my answer – Bainternet Aug 19 '12 at 13:35
  • i did use your last updae but still not working. is it something wrong in my mytheme_comment function ? is it possible for you to login to wp site and have a look at the code? :) – Zakir Sajib Aug 19 '12 at 13:41
  • i dont mind to pay :) – Zakir Sajib Aug 19 '12 at 13:58
  • sorry man its working. i put 'comments' in parameter, it should be 'comment'. thanks man. but now how about displaying comments in oldest to newest ? – Zakir Sajib Aug 19 '12 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.