Nope, this isn’t possible on the back-end, as this requires Conditionals (the two values have to have values in order to perform calculations).
However, if you’re just wanting to show it on the front-end, this is very possible with some PHP
{@ID,calc_field_less_other_field}
the function field_less_other_field would need to do the math calculation and pull the fields based on the post ID being passed. That could be as simple as:
function calc_field_less_other_field ($id) {
$field_one = get_postmeta($id,'field1',true);
$field_two = get_postmeta($id,'field2',true);
return $field_two - $field_one;
}
There’s obviously no error checking in this, and you would have to make sure your fields were represented as numbers and that you’re performing the math correctly, but this would be something you could test at your end easily.
Creating a Calculation Field doesn’t always make sense, because if the calculation will never change, your calculation will always be accurate if it’s handled in code based on the value of the other two fields.
Well, shoot, I’m not a PHP Developer, so apologize, but I typed get_postmeta
instead of the actual get_post_meta
. My bad!
Here’s the corrected code:
function calc_field_less_other_field ($id) {
$field_one = (int) get_post_meta($id,'field1',true);
$field_two = (int) get_post_meta($id,'field2',true);
$result = $field_two - $field_one;
return $result;
}
-
This reply was modified 6 years ago by Jim True. Reason: Fixing the PHP Code. I'm really not a PHP Developer ;)
-
This reply was modified 6 years ago by Jim True. Reason: Type casting the two variables to Integer. I always knew Hollywood PHP Casting Directors were prejudiced against Integers