ACF Repeater Query

Hi Guys

If running a normal Post Loop Query, to return the posts as cards, how would I then be able to check if ANY content was present in any of the fields in a given ACF Repeater, and show a button if so, but another button if not?

Could be over-complicating it, but just curious as nothing on here yet has seemed to handle that.

Thanks

1 Like

Hi

You would make a condition with {query_results_count:queryid} that checks if more than 0 items were found.

Query fields

{query_results_count} – Use inside or outside a query loop to return the query results count (@since 1.9.1). When used outside a loop you have to pass the query loop ID as a filter to this tag like this: {query_results_count:quer34} . “quer34” in this example is the query loop ID, which you can retrieve by copying the element ID of the query element into your clipboard. Make sure to remove the “#brxe-” prefix. You only want to use the last six-character ID.

That’s how I do it.

Cheers

Patric

3 Likes

use Condition Element Conditions – Bricks Academy

and dinamic tag {echo}

and ACF | have_rows()

example {echo:have_rows('rep_field')}

3 Likes

If you want to use standard Bricks… here is my example.

I have this condition on an icon outside / above the ACF Repeater. I only want to show this icon if the ACF Repeater below has more than 1 item.

This is the structure with the Toggle icon and the ACF Subrepeater Query below it:

Cond2

And the condition in the Toggle icon element:

This condition above will be true, if the ACF Repeater has more than 1 item.

Cheers

Patric

1 Like

Oh all those are really helpful, thanks all.

@Patric - So that will check if there’s 1 or more, in the loop (i.e on a given post/CPT you want to check if any of the repeater fields are filled, and if so, it’s true). Or would that be >= ?

TIA

Just change the 1 to 0.

The condition will then be true if at least 1 item in the ACF Repeater was found.

Cheers

Patric

Oh, interesting. That’s actually really obvious but I didn’t consider it.

Cheers Patric

1 Like

Related:
If you are looking to check the presence of rows in a repeater or flexible content related query, I use this:

WPCodebox2:

<?php 

function check_acf_flexible_content($field_name, $post_id = false) {
  // If a post ID is not provided, use the current post ID
  if (!$post_id) {
    $post_id = get_the_ID();
  }
  
  // Check if the specified field has any rows
  if (have_rows($field_name, $post_id)) {
    return true;
  }
  return false;
}

and in the builder conditions:
{echo:check_acf_flexible_content(insert_name_of_flexible_content_or_repeater_acf_field)}

2 Likes

Really helpful, thank you mate.

1 Like

Just for the sake of somebody else reading this, in your example there, as the Toggle icon is outside the loop it would then require the ID for that element in the condition too, right?

Hi

Yes, you are right, but I don’t have the ID in the element and it still works.

Maybe it works because that is the only query on the page. I don’t know.

Anyway, if you want to be sure, you can always put the ID into the element like mentioned in the Bricks academy tutorial.

Cheers

Patric