Expressions are the corner stone of Calculated fields for ACF, it’s what you enter in to the “Formula” field that this plugin adds to the ACF field editor.
The expressions syntax is designed to be as familiar to “normal usage” as possible, most people that have used Excel should be pretty comfortable with the way of entering expressions. An expressions will always return a value when they are evaluated, if the expression contains errors, it will silently return 0 (zero).
You can think of the content of the formula field as an assignment expression, the field will get the return value of the expression in the formula field. If an expression can’t be evaluated due to invalid syntax or referring to undefined field names, it will silently return zero.
The valid building blocks of an expression are the following parts:
This could be any number “hard coded” into the formula field. The simplest possible formula using a constant would be:
This formula will always of course always return the value 10, perhaps not very exciting.
Calculated Fields for ACF also has built in named constants that can be used:
The following built in constants are available:
|pi||The mathematical constant Pi. 3.1415…..|
|e||The mathematical constant e, the 2.71828 and is the base of the natural logarithm|
Note that when using these constants, they will override any field names. If you name another field to pi or any of the above built in constants, you will not be able to refer to that field in a formula.
An expression can pick up a value from another field on the same post, this is done by simply referring to the other field via it’s ACF Field Name:
The above expression will return the value of the field “price” to the calculated field. Note that when a field expression is evaluated the returned field value may also be the result of a previous calculation. Field expressions are calculated in the order that the fields appear in the ACF field editor. It’s therefore important that an expression only refers to fields that appear before / above it in the field order. If an expression refers to a field that is defined after / below it, the result will be unpredictable.
Operators are the things that actually perform calculations. A very simple expression using an operator is:
|price||A field expression. This is the name of another field. When this part of the expression is evaluated, the string “price” will be replaced with the current.|
|*||Multiplication operator. Multiplies the expression to the left with the expression to the right|
|10||A constant expression.|
Please see this section about operators to read more about all supported operators.
Calculated Fields for ACF also supports simple functions as part on an expression. A function always has the form: name(parameter). An example expression containing a function:
The above expression will return the value of price rounded to the nearest integer value. Note that all functions in Calculated fields for ACF always take exactly one (1) parameter, this is a simplification that might change in the future.
For more detailed control of the calculation order, an expression can use parentheses. The content inside the parentheses is evaluated as a separate expression.
(2 + 10) * 3is evaluated in 2 steps. In step 1, the expression in the parentheses is evaluated so it becomes:
(12) * 3
In step 2, the resulting expression is evaluated
12 * 3 = 36
Parenthesis may be nested in as many levels as needed. For instance
((((12*4) * 2) *3) *4) is a valid expression.