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

Leave the first comment

 

Related Tutorials..

Pro
Filtering Meta Box Cloneable Group by Select Subfield for Multiple Bricks Query Loops with Conditional Output

Filtering Meta Box Cloneable Group by Select Subfield for Multiple Bricks Query Loops with Conditional Output

In the Bricks Facebook group a user asks: Consider this cloneable Meta Box field group for a Custom Post Type called Tour: with the Tour…
Pro
Conditional Output based on Date Time Picker Field in Bricks

Conditional Output based on Date Time Picker Field in Bricks

In the past, we showed how elements can be conditionally output based on a post's Date type of ACF field here. This Pro tutorial for…
Categories:
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…
Pro
Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

How to register a new dynamic tag for setting excerpt word limits and outputting an ellipsis (...) at the end.
Categories:
Pro
Programmatically populating ACF field values in WordPress

Programmatically populating ACF field values in WordPress

An example of how to set the values of a Select-type field with the names and labels of all public post types.
Categories:
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
Filtering ACF Relationship Query by Post Meta in Bricks

Filtering ACF Relationship Query by Post Meta in Bricks

How to filter the posts of a post type related to another post type based on the value of a True / False type ACF…