Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort comments by votes #3

Open
davekonopka opened this issue Nov 21, 2011 · 2 comments
Open

Sort comments by votes #3

davekonopka opened this issue Nov 21, 2011 · 2 comments
Labels

Comments

@davekonopka
Copy link
Owner

Add an option to sort comments on a selected post by number of votes.

@jmorais
Copy link

jmorais commented Oct 24, 2012

Old ticket, but +1

@jmorais
Copy link

jmorais commented Oct 24, 2012

I just figured out how to do it: in updownupdown.php we will need these two functions:

function up_down_comment_votes_only_total( $comment_id ) {
    global $up_down_plugin;
    if ( !$comment_id )
        return 0;   
    $vote_counts = $up_down_plugin->get_comment_votes_total( $comment_id );
    return $vote_counts["up"] -  $vote_counts["down"] ;
}

function comment_comparator($a, $b) {

    $cmp = 0;

    if ($a->comment_ID != $b->comment_ID) {
    $votes_a = up_down_comment_votes_only_total( $a->comment_ID );
    $votes_b = up_down_comment_votes_only_total( $b->comment_ID );
    if ($votes_a != $votes_b)
        $cmp = $votes_a > $votes_b?-1:1;
    else
        $cmp = strtotime($a->comment_date) < strtotime($b->comment_date)?-1:1;
    }

    return $cmp;

}

And then, on your comments.php, we need:

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

Where your_callback_name is the function you use to render your comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants