0

I have created custom fields for comments with ACF, and I am doing calculation for comments. It works well but for single comment. I would like to calculate all values from specific field for all comments.

For example

$number1 = get_field('number1',$comment)
$number2 = get_field('number2',$comment)
$number3 = $number1 + $number2

I have this code and it does the calculation for single comment. Now I would like to get values for that fields from all comments for that post, and do the calculation like

$number_total = $all_numbers / $number_of_fields

Is that possible?

$total = $var1+ $var2+ $var3+ $var4+ $var5;
$stat= $total /5;
}
echo $stat; echo '/100'; 

And the var1,var2,var3 are advanced custom fields

if( have_rows('safety',$comment) ):
    while ( have_rows('safety',$comment) ) : 
        the_row();
        the_sub_field('field1');
        $field1= get_sub_field('field1');
        $safety_points= $field1;
        $safety_total= $safety_points/5*100;
        echo $safety_total;
    endwhile;
    else :
endif;

closed as off-topic by Michael, Mark Kaplun, Jack Johansson, Howdy_McGee Aug 18 '17 at 3:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please provide the code for how you are rendering the comments. You will need to loop through each comment, get the field values and add them to an overall array that you can use later to get your totals. – gdaniel Aug 9 '17 at 16:30
1

To do calculations with all the comments, you have to fetch them first, and run a loop to do whatever you wish (which you didn't clearly state, so I can't write an accurate answer).

To do so, you can use get_comments():

// Get the comments for the current post
$comments = get_comments();
// Run a loop and do stuff
foreach( $comments as $comment ){
    // Do your calculation here. You can access the
    // comment's ID by using $comment->ID;
}

Take a look at the codex page that I provided, to see a full list of possible parameters and returned values.

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