24th Mar '23
/
16 comments

Checking if Repeater/Cloneable Meta Fields Are Empty in Bricks

Plugins like ACF, Meta Box and JetEngine enable us to register custom meta for posts in WordPress that are repeating/cloneable. Ex.: A Technology field that has say, Technology Name and Technology Age sub fields.

There could be times when you want an element say, a Bricks Section that contains such repeater fields to be rendered only if there is at least one row in the field i.e., if the field is populated with at least 1 set of sub field values.

We can create a custom function that returns the count of array items and use this with the Dynamic data condition in Bricks to ensure that the Section gets output only if at least one row of data is filled in.

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

function bl_get_repeater_count( $field_name ) {
	return count( get_post_meta( get_the_ID(), $field_name ) );
}

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

and apply a condition like this:

{echo:bl_get_repeater_count(technologies)}

Replace technologies with the name/ID of your repeating custom field.

Get access to all 633 Bricks code tutorials with BricksLabs Pro

16 comments

  • Cheok Way Leong

    Hi Sridhar, I tried doing this with 2 different Post Types linked with relationship. If relationship is applied, for example, display Post Type B's Custom Field Repeater Group in Post Type A's Single Post Page does not work for this method. Is there anything that I needed to add in the php functions for this situation?

  • Eric Embacher

    This isn't working for me. No matter how many records in in the repeater, whether zero or 3 records, it always returns 1. Here's an example of my ACF repeater field "videos" that this function returns 1 for: https://www.dropbox.com/s/nmb3x14b3df7gqv/Screenshot%202023-07-23%20at%2010.10.56%20PM.png?dl=0

  • Domenico Mastroianni
  • Domenico

    Hey Sridhar, thanks for sharing this solution. I tried to implement it but I am not getting the condition to work. I have copy/pasted the php code into a snippet. Set the condition on my div to show it only if my repeater (JetEngine) called 'doorbird' is > 0. Where did I get lost in this simple tutorial?

Leave your comment

 

Related Tutorials..

Pro
Conditional Rendering of ACF Repeater Rows Based on True / False Sub Field in Bricks

Conditional Rendering of ACF Repeater Rows Based on True / False Sub Field in Bricks

This Pro tutorial provides the steps to modify a ACF Repeater query loop to only output the rows for which a True / False type…
Categories:
Pro
User Role Conditional Output in Bricks

User Role Conditional Output in Bricks

This Pro tutorial provides the steps to output elements based on their CSS class to users of the specified role in WordPress when using Bricks…
Categories:
Pro
Conditional Section when Post has a Featured Image in Bricks

Conditional Section when Post has a Featured Image in Bricks

This Pro tutorial provides the steps to output a Section in Bricks on single posts (of any post type) only if the post has a…
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…
Categories:
JetEngine Checkbox Condition in Bricks

JetEngine Checkbox Condition in Bricks

How elements in Bricks can be conditionally output depending on the selected option from a JetEngine checkbox field.
Categories:
Tags:
Conditional Rendering for Logged in/out Users by CSS Classes in Bricks

Conditional Rendering for Logged in/out Users by CSS Classes in Bricks

This tutorial shows how Bricks builder's bricks/element/render filter can be used to conditionally output elements having a class of logged-in to logged-in users only and…
Categories: