Repeater and group fields

Calculated Fields for ACF supports working with repeater fields (requires Advanced Custom Fields Pro) and group fields.

Standard usage

If a formula expression is added to a subfield inside a repeater or group field, any field name in the formula expression is assumed to be another subfield in the same repeater or subgroup.

img

Screenshot from a repeater field. Any formula expression in field "p1" or "p2" can refer to the "price" field. The same logic applies to group fields.

Accessing parent level data inside a repeater or group

A formula expression in one of the subfields in a repeater or group field may refer to fields on the parent level using the prefix "parent" and a dot:

img Assuming the parent level has a field named rebate.

The expression "parent.prefix" is a legal expression and can be used together with functions and operators just like any other field/variable expression.

Aggregation functions for repeater fields

On the parent level, the values in a repeater field can be accessed using one of the aggregation functions.

Function Description
sum() The sum of the subfield for all rows in the repeater.
average() The average of the subfield for all rows in the repeater.
count() The count of rows in the repeater.
min() The minimum value of the subfield for all rows in the repeater.
max() The maximum value of the subfield for all rows in the repeater.

img An expression that sums up the values in subfield "price" for all rows in the repeater "orderlines"

A little trickery

To make the aggregation function work out, we’ve bent the normal rules of valid expressions a little bit. Anything after the dot (.) in the aggregate expression is an expression in itself that is evaluated for each row in the repeater. Therefore it’s perfectly OK to write a formula like:

img

This expression will walk over all lines in the “orderlines” repeater field, evaluate the expression “price * amount” and return the sum of all lines.

The side effect of this is that when working with aggregate functions, parentheses cannot be used in the expression. If you need to aggregate a more complex calculation, you should add an extra field in the repeater group and let the aggregate function work on this extra field.