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;
``````
• 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. Commented Aug 9, 2017 at 16:30

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