1

I am using ACF Plugin in my wordpress site, and I have added radio buttons in comment form, like reactions. comment form

it's saving results in wp_commentmeta like this;

wp_commentmeta

it's working very good but I want to display results in single post page block below the post content like this ;

sad: 44

funny: 11

hate: 23

good: 0

is it possible? Thanks for answers!

1

Put the following function in your Theme's functions.php and then put <?php echo tnc_reactions_count($post->ID); ?> in your single.php to output the numbers. Let me know if it works as I have not tested it completely.

function tnc_reactions_count($post_id){
    global $wpdb;
    $comments_table = $wpdb->prefix.'comments';
    $commentsmeta_table = $wpdb->prefix.'commentmeta';
    $get_post_comments = $wpdb->get_results( "SELECT * FROM $comments_table WHERE comment_post_ID='$post_id'", OBJECT );
    $funny_total = 0;
    $sad_total = 0;
    $hate_total = 0;
    $good_total = 0;

    foreach ($get_post_comments as $key => $value) {
        $comment_id = $value->comment_ID;
        $query_reaction = $wpdb->get_results( "SELECT * FROM $commentsmeta_table WHERE `comment_id`='$comment_id' AND `meta_key`='comments_reactions'", OBJECT );

        foreach ($query_reaction as $key => $value) {

            $get_reaction = $value->meta_value;

            switch ($get_reaction) {
                case 'funny':
                    $funny_total += 1;
                    break;

                case 'sad':
                    $sad_total += 1;
                    break;

                case 'hate':
                    $hate_total += 1;
                    break;

                case 'good':
                    $good_total += 1;
                    break;
            }
        }
    }
    $output = '<h3>Reactions</h3>';
    $output .= 'Funny: '.$funny_total;
    $output .= 'Sad: '.$sad_total;
    $output .= 'Hate: '.$hate_total;
    $output .= 'Good: '.$good_total;

    return $output;
}

Thanks

  • thank you very much for answer, but always "0" do I need to change any part of function? – Ayd In Mar 12 '17 at 11:37
  • There was a mistake. Please try again with the edited code. – Abdul Awal Mar 12 '17 at 12:24
  • It's same brother – Ayd In Mar 12 '17 at 15:32
  • Ok. I'll test it and post again – Abdul Awal Mar 12 '17 at 15:33
  • can I add the count numbers in postmeta? when users write a comment and selected reaction? with update_post_meta ? – Ayd In Mar 12 '17 at 15:35

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.