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:

<?php

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:

{acf_scientific_coordinators_name}{acf_scientific_coordinators_institution}

and the output on the Page would be:

  • Paul Miles
  • Ezekiel Deleon, Nalanda

Reference

https://www.advancedcustomfields.com/resources/acf-format_value/

Get access to all 537 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Category Grid with ACF Images in Bricks

Category Grid with ACF Images in Bricks

Creating a grid of post categories, each category card showing an image from an ACF field, category name & description.
Categories:
Pro
Random images from ACF Options Gallery in Bricks

Random images from ACF Options Gallery in Bricks

This Pro tutorial provides the steps to output images from an ACF Gallery-type custom field in a Bricks query loop randomly with each image appearing…
Categories:
Pro
How to Update Custom Fields with Default Values For Existing Posts when using ACF

How to Update Custom Fields with Default Values For Existing Posts when using ACF

Let's take a scenario where there are 10 Pages in a WordPress site. You proceed to create a Text-type of custom field called, say, "Custom…
Categories:
Tags:
Pro
ACF Checkbox Sub Field as Unordered List in Bricks

ACF Checkbox Sub Field as Unordered List in Bricks

Updated on 22 Sep 2023 This Pro tutorial shows how the checked options of a Checkbox-type of Sub Field inside a ACF Repeater field can…
Categories:
Pro
Sorting ACF Relationship Posts by Date Meta in Bricks

Sorting ACF Relationship Posts by Date Meta in Bricks

Consider the following setup: CPT: Events ACF Fields:Event Date Information (Group)|__Event Date (Date Picker)Pick Sub Events (Relationship) An Event can have 1 or more related…