Support

Account

Home Forums Feature Requests ACF total sum

Solved

ACF total sum

  • Hello guys, i have searched for days a answer for this, but no luck.
    So, here is my problem:

    I have a custom field called ‘price’ , where i add the price of the item i am talking about on my site.

    I would like to create a function to calculate and display the sum of all ‘price’ fields from all my posts.

    Can someone help me with this?

    Thank you in advance!

  • i comed up with this code, but it only calculates the sum of the posts on that page or category

    $factorTotal = 0;
    $factorCount = 0;

    while ( have_posts() ) : the_post();
    $factorI = get_field(‘price’, $post->ID);
    if($factorI){ // Only if it exists
    $factorTotal += $factorI; // Add it
    $factorCount++; // Count it
    } // end if
    endwhile;

    echo ‘Total Value: ‘. $factorTotal .’ $’;

    I need it to calculate the sum of custom field ‘price’ that is in all of my posts

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘ACF total sum’ is closed to new replies.