Display Custom comments field number

I have some custom comments fields, I need to display the number of times that a meta value is used

Example :

Read More

I have a custom comments field “Do you like this post” with multiple choice :

  • Yes

  • No

I need to display how much person say “Yes”, how much say “No” for each post, It’s an example. Don’t tell me to use “post rating” plugins 😉

Related posts

Leave a Reply

1 comment

  1. I would try something like this:

    $comments = get_comments();
    $yes = 0;
    $no = 0;
    foreach ($comments as $comment) {
        $vote = get_comment_meta ( $comment->comment_ID, 'vote', true );
        if ($vote == 'yes') {
            $yes++;
        } else if ($vote == 'no') {
            $no++;
        }
    }
    echo 'Yes:'.$yes.' No:'.$no;
    

    Problem is the numbers can easily be manipulated so it might be better to count only one vote per user_id, comment_author or comment_author_IP depending on your usage.

    Codex: get_commentsget_comment_meta