Adds a formula fields ot Advanced Custom Fields that allows you to perform simple math based on field values. Calculations are done server side when a post is saved.
Calculated fields requires Advanced Custom Fields 5.0 or later and works with Pro as well as the standard version.
Calculated fields adds a new setting “Formula” to the the ACF field editor. Formulas supports refering to other fields in the same Field group using the field name. If a formula is added to a sub field inside a repeater field, the name refers to another sub field in the same repeater.
A formula can contain the basic mathematical oprerations: plus (+), minus (-), multiply (*), division (/) and power of (^). Formulas can also contains functions like sin, cos, arcsin, log, ln, sqrt, abs. Parantheseis are supported.
If an expression can’t be evaluated due to invalid syntax or refering to undefined field names, it will silently return zero.
Examples of valid expressions (note, no equal sign at the beginning):
* fieldA * 2
* fieldA * fieldB
* abs(fieldA) * (2 + sqrt(fieldB))
Array functions for repeater fields
If repeater fields are used, a calculated field in the parent field group can summarize a specific repeater sub fields using the aggregation functions sum, count, average, min or max. For example, the expression: “Sum(orderlines.amount)”” will return the sum of all the “amount” fields in the repeater field “orderlines”.
Note that anything after the dot (.) in the aggregate expression is an expression in itself. It’s perfectly OK to write a formula like: “Sum(orderlines.price * amount)”. This expression will walk over all lines in the “orderlines” repeater field, perform the calculation “price * amount” and return the sum of all lines.
Note that when working with aggregate functions, parantheses can not be used. If you need to aggregate a more complex calculation, you should add an extra field in the repeater group (hidden if needed) and let the aggregate function work on this extra field.
Calculations are made as the custom field is stored to the database. To minimize the impact of performance, Calculated fields rely on field order. Any formula field must be ordered AFTER the fields it refers to. Using fields that are defined before your calculated field WILL PRODUCE UNPREDICABLE RESULTS.