Outputting Only the First ACF Repeater Row in Bricks

In the Bricks Facebook group a user asks:

How can I display only the first entry from an ACF repeater?

This can be done using the bricks/query/run filter through which all query types other than Posts, Terms and Users like ACF Repeater and ACF Relationship (Pro version of ACF is needed for Repeater and Relationship fields) go through. This enables us to modify the array that the loop iterates through and restrict it to just the first one, in this case.

Add this in child theme‘s functions.php or a code snippets plugin:

add_filter( 'bricks/query/run', function( $results, $query_obj ) {
  if ( $query_obj->element_id !== 'uxigqi' ) return $results;

  // return the first result
  return array_slice( $results, 0, 1 );
}, 30, 2);

Replace uxigqi with the Bricks ID of your query loop element.

Note: We must ensure that our anonymous callback function is hooked to the bricks/query/run filter at a lower priority than 10 i.e., have a value higher than 10. This will make our callback be executed later or after the core Bricks callback thus effectively overriding it by it taking precedence.

Before:

After:

Powerful filters such as these are one of the reasons that make Bricks a developer’s dream.

Thanks to Jenn Lee for reminding me the filter name.

  • Andre beltrame

    It’s possible a tutorial using the first taxonomy?

    • A

      Can you elaborate? What type of query does your query loop use? Terms? and if so, are you trying to output just the first one?

Leave your comment