1

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

Example :

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

  • Your question isn't clear. You want the number of times the meta_value is set, whether "yes" or "no"? The number of "yes" votes for all posts? The number of "yes" votes per post, or for a single post? – s_ha_dum Mar 27 '13 at 14:08
0

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_comments - get_comment_meta

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.