This is a classic for me!
Many times I need to makes some mathematics with custom fields, for example, if I need to sum two values stored in two custom fields, or I want to calculate a percentage based on two values or any mathematics operation.
I can add this shortcode:
// Calculate shortcode add_shortcode('wpv-calculate', 'calculate_shortcode'); function calculate_shortcode($atts) { return wpv_condition($atts); }
Then I can use this shortcode anywhere:
[wpv-calculate evaluate="..."]
The attribute “evaluate” can get any mathematics operation and use custom fields as variables. For example:
[wpv-calculate evaluate="([custom-field-1] + 5) * [custom-field-2]"] [wpv-calculate evaluate=" [custom-field-1] / [custom-field-2] * 100 "] %
It is also possible to round the result adding PHP round() to the function return:
// Calculate and round shortcode add_shortcode('wpv-calculate', 'calculate_shortcode'); function calculate_shortcode($atts) { $atts = wpv_condition($atts); return round($atts); }
round() reference: https://www.php.net/manual/es/function.round.php
You can find this solution on this Toolset forum thread:
https://toolset.com/forums/topic/calculating-with-field-values/