WordPress comment sort order

Webmasters! Check out these money makers!!!!

Banned from Google? Check out this post!

Sort comment display order (ascending) ASC or (descending) DESC in WordPress 2.6.

I found this information on the web that was written about a year ago. Kudos to whomever hypOr is!

After searching through wp-includes/comment-template.php I found where to implement the mod. Works with my WP v2.6 no problem. However, when it comes time to upgrade your 2.6, you will have to do this again provided the future versions don’t change this one particular function. Future versions may even include this in the admin area. I hope. 😉

Around line 642 in wp-includes/comment-template.php you need to add:

, $order = 'DESC'

after:
$file = '/comments.php'

Example:

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

No mods to these areas:

{
global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity;

if ( ! (is_single() || is_page() || $withcomments) )
return;

$req = get_option('require_name_email');
$commenter = wp_get_current_commenter();
extract($commenter, EXTR_SKIP);

/** @todo Use API instead of SELECTs. */
if ( $user_ID) {

Then on ~lines 654, 656 and 658 add:

$order

after:
ORDER BY comment_date

Example:

$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) )

ORDER BY comment_date $order",

$post->ID, $user_ID));
} else if ( empty($comment_author) ) {
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'

ORDER BY comment_date $order",

$post->ID));
} else {
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) )

ORDER BY comment_date $order",

$post->ID, $comment_author, $comment_author_email));
}


Or you can just replace the whole section if you want. Look carefully but, this should start at line 642 and end at line 659

function comments_template( $file = '/comments.php', $order = 'DESC' ) {
global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity;

if ( ! (is_single() || is_page() || $withcomments) )
return;

$req = get_option('require_name_email');
$commenter = wp_get_current_commenter();
extract($commenter, EXTR_SKIP);

/** @todo Use API instead of SELECTs. */
if ( $user_ID) {
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) ) ORDER BY comment_date $order", $post->ID, $user_ID));
} else if ( empty($comment_author) ) {
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date $order", $post->ID));
} else {
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) ORDER BY comment_date $order", $post->ID, $comment_author, $comment_author_email));
}


That’s it! Works for me…

Here is the link to where I found this.

After all that I found THIS. But, I have not tried it.

Webmasters! Check out these money makers!!!!

Banned from Google? Check out this post!

This is so amazing I had to put it here.
Get the kit HERE!
water-for-gas.jpg

3 Responses to “WordPress comment sort order”

  1. Greetings….. | Zentode on August 16th, 2008 8:46 am

    […] through mine here you may want to check out these money makers!!!! You will also find a handy WordPress comment sort order tweak there too. All at […]

  2. Gerro on September 22nd, 2008 10:04 pm

    This could work, but if you just want the sort to work for a specific theme, I suggest sticking to the comments.php.

    I’ve found a quick fix that requires only a single line of code to be added on the comments.php to work.

    I posted in my blog a short article on the solution to the worpdress comment sort.

  3. Zentode on September 23rd, 2008 8:08 am

    Excellent solution. Thanks. I will have to try this. Way better than having to refer to notes on what to change with every PHPWS upgrade. Thanks for the post.

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!






Hosted by the creator. Want one? $100 a year! Contact Me   Activism Blogs - BlogCatalog Blog Directory