26th Nov '22
/
16 comments

Checking for ACF Repeater Rows in Bricks

Updated on 13 Feb 2025

Bricks enables us to conditionally output elements if a ACF field of basic type like Text has value by selecting it in the Dynamic data condition with != and leaving the value input empty.

With the above condition applied, the element will only be output if the custom field named header_notice created with ACF is not empty.

What about the case when the field type is a little more complex one (that returns an array) like the ACF Repeater?

We need to define a custom function that returns either true (1) or false (0) and check against = 1 (to check if not empty) or ! = 1 (to check if empty).

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

// Function to check if the given ACF Repeater field has any rows of data.
function bl_have_rows( $field ) {
  // if ACF is not active, return false
  if ( ! class_exists( 'ACF' ) ) {
    return false;
  }

  return get_field( $field ) ? true : false;
}

Whitelist the bl_have_rows function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_have_rows'
  ];
} );

You should also add other functions (native or custom) being used in your Bricks instance besides bl_have_rows. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

Finally use it like this:

{echo:bl_have_rows(site_builders)}

Replace site_builders with the name of your ACF Repeater’s field name.

With the above condition in place, the Section will be output only if a ACF Repeater named site_builders has at least 1 row of data.

Get access to all 610 Bricks code tutorials with BricksLabs Pro

16 comments

  • Joseph Orr

    I have tried every possible way to try to show/hide certain elements and cannot for the life of me get any of it to work at all on repeater fields. If the repeater field is nested within a container, will it still work? Please let me know if you can lend a hand - I feel really dumb that I cannot figure this out after like 10 hours of reading documentation.

  • Nicolas

    Would be nice to add it to BricksExtras optional additional conditions

  • Maddalena Fasser

    I tried to check ACF Repeater's sub fields as well with this method, but wasn't successful so far. Would you consider extending the tutorial to include the sub fields? Thanks!

  • Great tip - thank you!

    Following on from this, if I am using the Bricks Slider, let's say, and relationship fields (to display testimonials in the Bricks Slider - very common) how can I query the relationship field to check the amount of selected content? (i.e 1, 3, 5 testimonials to show).

    I ask this as the current slider loops through the content and creates multiple slides, even if it's just one testimonial. I am trying to query it so I create a different loops depending on the 'count' of the content chosen :) (i.e == 1 or < 1).

    :value sadly didn't work..

  • Simone Schregenberger

    This is an addition for people who added an acf repeater to a term/taxonomy and want to check the repeater rows on the archive page:

    Add get_queried_object() to the get_field() function.

    function bl_have_rows( $field ) { // if ACF is not active, return false if ( ! class_exists( 'ACF' ) ) { return false; } return get_field( $field, get_queried_object() ) ? true : false; }

  • Bradley Gregory

    FYI - I got this to work for flexible content using has_rows function. Is there any reason we should choose the class check vs the has rows?

    • A

      Class check is not compulsory but a good thing to have just in case ACF-specific functions are being called when ACF is deactivated for whatever reason. But practically speaking, it is ok in most cases to just use since the likelihood of the required plugin not being active is rare.

  • Domenico Mastroianni

    I’ve got a couple of website using jetengine and would love to have the ability to control the condition for their repeaters.

  • Domenico Mastroianni

    Would this code work only with ACF or other similar plugin that creates repeaters?

    • A

      only ACF.

      Which custom fields plugin do you use?

      • Domenico

        oh dear, I missed your reply. I was and am using JetEngine in this particular instance. I still cannot use conditional visibility on repeaters/sub fields.

Leave your comment

 

Related Tutorials..

Pro
Conditional Output Based on ACF Date Field in Bricks

Conditional Output Based on ACF Date Field in Bricks

Updated on 1 Aug 2024 Looking to have a Section or other elements in Bricks rendered on the frontend only if today's date is before…
Categories:
Pro
ACF Group Sub Field Values from Matching Post Term

ACF Group Sub Field Values from Matching Post Term

In Bricks Reddit, a user asks: Let's break this down and set up the scenario practically. First, there's a Language taxonomy for posts (could be…
Categories:
Pro
Sub Field Value from the Last ACF Repeater Row in Bricks

Sub Field Value from the Last ACF Repeater Row in Bricks

Getting the value of a specific sub field of an ACF Pro‘s Repeater field and output in the query loop.
Categories:
Tags:
Pro
City-based and Country-based Conditional Output in Bricks

City-based and Country-based Conditional Output in Bricks

This Pro tutorial provides the steps to render a Bricks element only if the visitor is from the specified country or city. Note: Caching may…
Categories:
Pro
Bricks Slider Images From ACF Flexible Content Sub Field

Bricks Slider Images From ACF Flexible Content Sub Field

Consider the following field group associated with Pages when using ACF Pro: "Page Fields" field group|_"Page Content" Flexible Content field|__"Layout 1" layout|___"Title" Text field|___"Layout 1…
Color Custom Field as Section Background Color on Category/Term Archives in Bricks

Color Custom Field as Section Background Color on Category/Term Archives in Bricks

How to use a color from a taxonomy term custom field for as the hero section background color on archive pages.
Categories:
Tags:
How to add a “Sold Out” badge for out of stock products in Bricks

How to add a “Sold Out” badge for out of stock products in Bricks

Looking to add a "Sold Out" badge for products in the Shop page for WooCommerce products that are out of stock when using Bricks' Products…
Pro
Featured Image’s Meta in a Bricks Posts Query Loop

Featured Image’s Meta in a Bricks Posts Query Loop

How to output values of custom fields for the featured image of current post in a query loop
Categories:
Tags: