Sort comments by karma
Question
I'm using the plugin comments rating (thumbs up - thumbs down) on my comments template. It stores "karma" in the db column "comment_karma" in comments table. I am looking for a way to sort wp_list_comments by higher karma to lowest.
Have tried something like
<?php wp_list_comments('callback=mu_custom_callback&orderby=comment_karma&order=DESC') ?>
but it's not working.
thanks
UPDATE
I got it working placing in functions.php `
function comment_comparator($a, $b)
{
$compared = 0;
if($a->comment_karma != $b->comment_karma)
{
$compared = $a->comment_karma < $b->comment_karma ? 1:-1;
}
return $compared;
}
and in comments.php
global $wp_query;
$comment_arr = $wp_query->comments;
usort($comment_arr, 'comment_comparator');
wp_list_comments('callback=gtcn_basic_callback', $comment_arr);
Found on this website
Solution
Just incase if you have not checked out the plugin developers page but heres something that you could try
if (function_exists(ckrating_get_comments)) {
$post_id = $post->ID;
$mycomments = ckrating_get_comments(
"post_id=$post_id&status=approve&
orderby=comment_karma&order=DESC");
}
else
$mycomments = null;
wp_list_comments(array(), $mycomments);
UPDATE
I got it working placing in functions.php I got it working placing in functions.php `function comment_comparator($a, $b)
function comment_comparator($a, $b)
{
$compared = 0;
if($a->comment_karma != $b->comment_karma)
{
$compared = $a->comment_karma < $b->comment_karma ? 1:-1;
}
return $compared;
}
and in comments.php
global $wp_query;
$comment_arr = $wp_query->comments;
usort($comment_arr, 'comment_comparator');
wp_list_comments('callback=gtcn_basic_callback', $comment_arr);
Found on this website
OTHER TIPS
@ Andycap Thats exactly what i am looking for. but somehow it doesn't work for me. I am pretty shure that i am doing something wrong. Can you explain where exactly to put the code in the functions.php and the comments.php. do i have to add your code or replace it?