Checking for ACF Repeater Rows in Bricks

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;
}

and 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 having a field name of site_builders has at least 1 row of data.

Get access to all 526 Bricks code tutorials with BricksLabs Pro

14 comments

  • 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.

      • Bradley Gregory

        Ah, that is a really good point. Thanks!

  • 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?

Leave your comment

 

Related Tutorials..

Pro
ACF Flexible Content in Bricks with Each Row in its Own Section

ACF Flexible Content in Bricks with Each Row in its Own Section

In the previous ACF Flexible Content in Bricks tutorial we showed how Bricks' Flexible Content type of query can be used for showing the ACF…
Read more โ†’
How to create parallax effects using Rellax.js in Bricks

How to create parallax effects using Rellax.js in Bricks

This tutorial provides the JS & PHP codes that can be pasted in order to create smooth parallax effects using the Rellax.js library inside Bricks…
Read more โ†’
Categories:
How to create a dynamic infinite logo slider with ACF and Bricks

How to create a dynamic infinite logo slider with ACF and Bricks

This tutorial provides the builder settings, PHP & CSS codes that can be pasted in order to create an infinite logo slider in pure CSS…
Read more โ†’
Conditionally Outputting Elements Only for Posts That Have a Specific Taxonomy Term

Conditionally Outputting Elements Only for Posts That Have a Specific Taxonomy Term

Using the core WordPress has_term() function for checking if the current post has a specific term of a given taxonomy.
Read more โ†’
Categories:
Pro
User Role Condition in Bricks

User Role Condition in Bricks

This Pro tutorial provides the steps to conditionally output elements depending on the currently logged-in user's role. Step 1 Create a section Template having the…
Read more โ†’
Categories:
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…
Read more โ†’
Pro
Conditional Rendering Based On Meta Box Group’s Sub Field

Conditional Rendering Based On Meta Box Group’s Sub Field

This Pro tutorial shows how we can output an element on a single post pages only if a specified sub-field (custom field created using Meta…
Read more โ†’
Pro
ACF Repeater Row Count Condition in Bricks

ACF Repeater Row Count Condition in Bricks

This Pro tutorial provides the steps to output elements on single posts (of any post type) in Bricks only if a specific ACF Repeater-type of…
Read more โ†’
Categories: