David Alsbright
David Alsbright

Reputation: 1575

Order wp_list_comments by popularity

I have a plugin to add a 'like' to a comment, each 'like' is stored in a table called likes_comments, I'm trying to sort the output of wp_list_comments according to how many likes each comment has, I'd like the comment with the most likes to appear at the top.

Here's what I'm using to call wp_list_comments:

<?php global $wp_query;
    $comment_arr = $wp_query->comments; usort($comment_arr, 'comment_comparator');
    wp_list_comments('callback=my_callback', $comment_arr);
?>

And here's my function:

function comment_comparator($a, $b)
{
$compared = 0;
if($a->likes_comments != $b->likes_comments)
{
    $compared = $a->likes_comments < $b->likes_comments ? 1:-1;
}
return $compared;
}
if($a->likes_comments == 0)
{
$compared = $compared2;
}

I'm completely stuck with this, any help at all would be greatly appreciated.

Upvotes: 0

Views: 1332

Answers (1)

Bud Damyanov
Bud Damyanov

Reputation: 31889

In order this to work, place the code in functions.php AND in and in comments.php (you need to find the exact placement location, before calling wp_list_comments() in comments.php):

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;
        }

comments.php:

global $wp_query;
    $comment_arr = $wp_query->comments;
    usort($comment_arr, 'comment_comparator');
    wp_list_comments('callback=gtcn_basic_callback', $comment_arr);

Important note: This procedure is valid when using the plug-in comments rating (thumbs up - thumbs down). Read additional info here.

Upvotes: 1

Related Questions