Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

1 Answer

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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