14th Dec '23
/
0 comments

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 633 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
How to programmatically add a class to an element in Bricks

How to programmatically add a class to an element in Bricks

This Pro tutorial shows how a dynamic class can be added to a query loop element in Bricks programmatically. Let's take an example where posts…
ACF Relationship: Count of Related Posts

ACF Relationship: Count of Related Posts

Looking to get the number of CPT B posts related to a CPT A post when using a Relationship field with ACF? Here's a custom…
Categories:
Tags:
Pro
CPT Submenu Items in ACF Pro Options Admin Menu

CPT Submenu Items in ACF Pro Options Admin Menu

This Pro tutorial provides the steps to add links to admin pages under an Options page created with ACF Pro. All code mentioned in this…
Pro
Filtering posts by a group field’s true / false field sub field value with ACF or Meta Box in Bricks

Filtering posts by a group field’s true / false field sub field value with ACF or Meta Box in Bricks

This Pro tutorial covers how posts of a Bricks query loop can be filtered to show only the ones whose true / false or checkbox…
Categories:
Tags: