Written on 6. July 2022 by Lennart Christensen

ACF: Create an ACF select field based on repeater fields from the same post

In this example, we use ACF repeaters from the same post to form the basis of a select field that can then be used. We use values from the repeater field to create the basis for the choices that the select field will contain. The select field is then unique for each post, based on the repeaters created on the post. In this example, the subfield from the repeater field is called "header". The repeater field itself is called "test-rows" and the select field in this example is called "budget_choice".

/* Add to your functions.php */

function acf_load_choices_from_repeater_lennartc( $field) {
	$choices = get_field('test_rows'); // we grab the repeater fields
	$field['choices'] = []; // we make an array for populate the choices
	if (is_array($choices)) { // we need this bcs otherwise we get a bug on pages where we cant find the repeaters, and in these cases we make a none
	foreach ( $choices as $choice ) {
		// vars
		$value = str_replace( ' ', '', $choice['heading'] ); // removing whitespace
		$label = $choice['heading'];
		// append to choices
		$field['choices'][ $label ] = $value;
	}
} else {
	$field['choices'][ 'none' ] = 'none';
}
	return $field; // return the field
}

add_filter('acf/load_field/name=budget_choice', 'acf_load_choices_from_repeater_lennartc');