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
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 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:
Pro
Conditional Rendering in Bricks Based on CSS Class

Conditional Rendering in Bricks Based on CSS Class

Updated on 19 Jun 2024 A user asks: Is there a way to apply the same conditions to several elements like we set styles appending…
Categories:
Condition for Checking Current Post Category in Bricks

Condition for Checking Current Post Category in Bricks

How we conditionally display an element based on the current post's categories.
Categories:
Conditionally Hide a Section on a Specific Taxonomy’s Term Archives in Bricks

Conditionally Hide a Section on a Specific Taxonomy’s Term Archives in Bricks

A user asked: I have a CTA in the footer of my site, which I have custom fields on various templates, etc to populate. I…
Categories:
Pro
Query Variables Condition in Bricks

Query Variables Condition in Bricks

This Pro tutorial provides the steps to register a custom function that returns true or false depending on whether the current page URL has specific…
[Function] Current Term Has Children

[Function] Current Term Has Children

Updated on 20 Jul 2024 A user asks: I'm working on an archive template for WooCommerce products. I'm trying to hide a section if there…
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.
Categories: