Modifying ACF Field Value Before It Is Output

Consider the scenario where a Page has a “Page Fields” field group created with ACF Pro like this:

Field name: scientific_coordinators (type Repeater)
|__ Sub field 1: name (type Text)
|__ Sub field 2: institution (type Text)

Sample Page being edited:

The objective is to output the name and institution separated by a comma for each row like this in a Bricks query loop with its query type set to the above Repeater when this Page is being viewed on the front end:

  • Paul Miles, Rutgers
  • Ezekiel Deleon, Nalanda

In the Bricks editor we could set the text of a Basic Text element like this:

{acf_scientific_coordinators_name}, {acf_scientific_coordinators_institution}

and it would work fine as long as the second sub field is not empty.

If the second field value is empty for one of the rows, the output would look like this:

  • Paul Miles,
  • Ezekiel Deleon, Nalanda

Not ideal.

We need a way to check if the second institution field has any value and only then output it with a preceding comma and space. Otherwise, nothing should be output.

This can be done using this code:


add_filter( 'acf/format_value/name=institution', function ( $value, $post_id, $field ) {
	return $value ? ', ' . $value : $value;	
}, 10, 3 );

Notice how we don’t prefix the field name with the group name. This means each field name must be unique for it to be targetted via a filter hook like the above. And so, the better practice would be naming the ACF sub fields scientific_coordinators_name instead of just name and scientific_coordinators_institution instead of just institution.

Anyway, with the above code in place we modify the Basic Text element’s text to:


and the output on the Page would be:

  • Paul Miles
  • Ezekiel Deleon, Nalanda


Instant access to 390+ Bricks code tutorials with BricksLabs Pro

Leave the first comment