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

    • A

      If you are using the Bricks' built-in Relationship query type, try applying this dynamic data condition: query_results_count.

      https://brickslabs.com/query-results-count-in-bricks/

      Are you using ACF or Meta Box?

      If ACF, see if this helps: https://brickslabs.com/acf-relationship-count-of-related-posts/

      • Cheok Way Leong

        Hi Sridhar,

        Would like to update on this, previously I mentioned that I got it worked. But don't know what happened, after awhile it doesn't work anymore.

        Inside the builder's structure panel (https://prnt.sc/v8yBe6xmPW_s), "Features - Nearby Amenities" is where the condition logic (query_results_count) is set. And "Feature List" is query looped with MB Relationship & "Feature Element" is query looped with MB Group Repeater

        • A

          Is prmygp Bricks ID of UL or LI?

          • Cheok Way Leong

            prmygp is Brick ID of UL, the element UL is query looped with Metabox Relationship. For the LI, the element is query looped with Metabox Group, which the MB group has a cloneable subfield text.

            After few tests and tries, I noticed that MB Relationship are able to to query loop the whole section as I would like to apply condition based on another Post Type's CPT > 0, therefore I have changed the query loop from UL to the whole section (https://ibb.co/9wTNhnn). I think following the tutorial you provide the condition logic should be correctly shown when the post is linked with another post type's post with relationship, but it could not detect whether there's data in another Post Type's CPT.

            I have tried of this 3 tutorials (this article, https://brickslabs.com/query-results-count-in-bricks/, https://brickslabs.com/acf-relationship-count-of-related-posts/), unfortunately that doesn't worked out for me.

            At the moment, I found another way to conditionally display. Which I create a button CPT with the choices of Disabled or Enabled, when the button is disabled, the section will be hidden and only shown if button is Enabled. (https://ibb.co/NVv5Nzp) But I still think the ideal way would be conditionally display based on > 0 arguments, hope that this could provide you more insights.

      • Cheok Way Leong

        Hi Sridhar, an update for your suggested tutorial. The function does work perfectly, it does work with Metabox AIO CPTs, as I don't have ACF, can't tested on it but I assume it will be working as well.

      • Cheok Way Leong

        I'm using Metabox AIO, I'll try this tutorial (https://brickslabs.com/query-results-count-in-bricks/) & update u again

  • 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 Related Posts in Bricks

Conditional Related Posts in Bricks

How to output a section only if there is at least 1 related post for the current post being viewed.
Categories:
Render an element having a specific HTML ID based on value of a custom field

Render an element having a specific HTML ID based on value of a custom field

This tutorial provides the steps to use Bricks' bricks/element/render filter to conditionally output an element that has the specified HTML ID based on the value…
Categories:
Pro
How to Insert Element(s) Between Query Loop Posts in Bricks

How to Insert Element(s) Between Query Loop Posts in Bricks

Update on 16 Aug 2023: Follow this tutorial instead. This Pro tutorial shows how we can insert a Div (or any custom HTML) after nth…
Conditional Output based on Manual Excerpt in Bricks

Conditional Output based on Manual Excerpt in Bricks

How to display a post’s excerpt only if it has been manually set.
Categories:
Weekday Condition in Bricks

Weekday Condition in Bricks

As of Bricks 1.7, the built-in "Weekday" condition does not work correctly. This is most likely due to it not taking the site's timezone (set…
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
Conditional CSS in Bricks based on Post Meta

Conditional CSS in Bricks based on Post Meta

In the Bricks Facebook group a user asked: Hi, how do you normally control margin, padding and font-size using dynamic data? I'm looking for the…