1

I have a custom post type named 'ordered_food' and the custom field named 'price', Alhough I can print out all the values, I don't know how to plus and calculate the total price. Because it's already in the wordpress custom post type loop.

Calculate all custom field values in the post loop

/* something like this */
$total_amount = $price_custom_field1 + $price_custom_field2 + $price_custom_field3; 
1
  • It helps to see the relevant code. There was a similar question here asked recently that might help you?
    – birgire
    Dec 11, 2015 at 13:28

1 Answer 1

1

Try this:

//loop start

    $total_amount = get_post_meta($post->ID, 'custom_field1', true);
    $total_amount += get_post_meta($post->ID, 'custom_field2', true);
    $total_amount += get_post_meta($post->ID, 'custom_field3', true);

//loop end  

    echo 'Total: '.$total_amount;   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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