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 sub field inside a repeater or group field, any field name in the formula expression is assumed to be another sub field in the same repeater or subgroup.

Screen shot from a repeater field. Any formula expression in field “p1” or “p2” can refer to the “price” field. The same logic applies for group fields.

Accessing parent level data inside a repeater or group #

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

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.

FunctionDescription
sum()The sum of the sub field for all rows in the repeater
average()The average of the sub field for all rows in the repeater
count()The count of rows in the repeater
min()The minimum value of the sub field for all rows in the repeater
max()The maximum value of the sub field for all rows in the repeater
Expression that sums up the values in sub field “price” for all rows in the repeater “orderlines”

A litte trickery #

To make the aggregation function work out, we’ve bended 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:

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 can not 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.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *