Visibility Condition using Repeater Nested in another Repeater

I have an ACF setup where a repeater is nested inside another repeater.

In the template designer, I can not currently get to the nested repeater in the visibility conditions dialog (see image attached). After selecting the first repeater, the next dialog only allows me to select inner fields not the nested repeater.

I would like to know if it is possible to set the visibility conditions for an item based on whether a field in the nested repeater is empty or not.

Hi @zagency,

are you able to select the Repeater in the dropdown where the ACF is selected at the moment GREEN?

grafik

Cheers

Hi @T-low

Thank you for your response, I also tried that, but I’m not able to select the repeater in that ACF dropdown. That would have worked, but the repeater doesn’t appear as an option (see image).

Hi @zagency,

oh right, sorry. In the Visebillety conditions, as far as I know, you can’t check for a layered repeater elements.

The only other solution I can think of would be a function return.

Cheers

Thanks @T-low,

Are you able to shed more light the how to achieve this using a function return?

@Louis and @Araminta your insights on this will also be appreciated?

Hello @zagency,

I might have misunderstood, as if we’re talking about a nested repeater, you would be testing for empty fields (repeated).

Something like this should do the trick (roughly set out, you might want to break out early, remove unnecessary steps if already valid etc…), AI commented. It will check if there is at least one repeated field that has a value.

/**
 * Check if a repeater field has any value for a specific sub field.
 *
 * @param string $field_name     The name of the repeater field.
 * @param string $sub_field_name The name of the sub field to check for value.
 * @return bool True if the repeater field has a value for the specified sub field, false otherwise.
 */
function acf_repeater_has_value( $field_name, $sub_field_name ) {
	$has_value = false; // Initialize variable to track if the repeater field has any value

	// Check if there are any rows in the repeater field
	if ( have_rows( $field_name ) ) {
		// Loop through each row of the repeater field
		while ( have_rows( $field_name ) ) {
			the_row();

			// Check if the current sub field within the repeater has a value
			if ( get_sub_field( $sub_field_name ) ) {
				$has_value = true; // Set to true if a value is found
			}
		}
	}

	return $has_value; // Return whether the repeater field has any value for the specified sub field
}

Cheers,

1 Like

Is it on the roadmap to be able to use visibility conditions with nested repeaters any time soon? If not, does any one have some direction on how to use @Louis provided function return code above?