Home Forums Add-ons Repeater Field Populate select field with values from repeater field


Populate select field with values from repeater field

  • I have a Post_Object field that (obviously) allows me to select a post object from a custom post type. Lets call this “Field A”. My custom post type has a repeater field that could have any number of entries given, the values added to the repeater field are essentially variations to “Field A”.

    On a different post type, I need to select a single variation that was entered into the repeater field for my original post type. Lets call the variation selection “Field B”.

    How can I populate “Field B” with repeater field values that are dynamically loaded from the selected post type value of “Field A”?

    I should mention, I read and it does not apply because my select values need to be populated based on the selection from a different field. I am sure AJAX will need to be involved somehow, but I am not exactly sure what direction to head with this.

    I hope that makes sense…

  • Hi @davidjustin

    This will be possible, but lets first forget about the AJAX side. Lets make it a bit more basic:

    1. The user selects a post type
    2. The user saves the post
    3. The user sees the select field populated with repeater values from the post type selected

    function my_acf_load_field( $field )
    	// globals
    	global $post;
    	// load selected value from post type field
    	$p = get_field( 'Field_B', $post->ID );
    	// now load repeater field from selected post object
    	if(get_field( 'Field_B', $p->ID ))
    		// loop through the repeater and use the sub fields "value" and "label"
    		while(has_sub_field('Field_B', $p->ID))
    			$value = get_sub_field('value');
    			$label = get_sub_field('label');
    			$field['choices'][ $value ] = $label;
        return $field;
    // acf/load_field/name={$field_name} - filter for a specific field based on it's name
    add_filter('acf/load_field/name=Field_A', 'my_acf_load_field');

    This is untested but should work. After you have it working, you can write some AJAX to fetch the values.

  • First let me say I am loving ACF. Totally awesome.

    Your response is great, and I can tell is pointing me in the right direction… I think I need to add some additional clarification though, because my need goes a little deeper. Hopefully I can clarify.

    So I have two custom post types. “House Models” and “Neighborhoods”.

    When I save a new House Model, I enter variations in a repeater field; the variations are called “Elevations”. The Elevations are not shared by different House Models.

    Then when I am editing a Neighborhood, it has a repeater field called “Lots”. Inside each Lot I add to the repeater, I need to select a House Model (Simply done with the post_object fiel and our “Field A”). Then I need to select which Elevation of the House Model was built on that Lot with a select field (Field B). Ideally Field B will be limited to ONLY the Elevations entered for the chosen Home Model in Field A.

    Where I feel stuck now, how do I populate Field B with the Elevations saved to the Home Model selected in Field A. The difficulty comes for me because of the multiple instances of Field A and Field B because they are inside a repeater themselves…

    Does that make it any clearer?

    I REALLY appreciate your help!!!

    – David Ray

  • Hi @davidjustin

    Hmmm… I see.

    I think the same code can still be used but with some extra tweaking.

    You use Field A (sub field) to select a “House Model”, field B appears as a blank select becuase no AJAX is hooked up yet.

    You save the page.

    The page re-loads and you hook into the load_field hook for field B (sub field next to Field A). In this hook, you can load all the values of the repeater field (“Lots”) and loop through the rows. You want to find the current row so I would write some code to pick out the row number for the $field[‘name’] and use this to match the loop index. This will get you the current row. Then you can find the Field A value and go from there,

    Pretty complicated stuff, but it is possible!


  • …I would write some code to pick out the row number for the $field[‘name’] and use this to match the loop index. This will get you the current row.

    Can you elaborate on this a little for me? I understand most everything up to this point.

    How do I find the row number while inside my function for the load_field on Field B?

    Thanks again!


  • Ok, after reviewing: I see that the row number is built in to the naming convention. I am still feeling a little lost though. If you can easily provide a code example that would sure help a lot 🙂


  • Hi @davidjustin

    Can you dump out the $field[‘name’] which inside the load_field filter we are talking about?

    Can you post the name here?


  • Hi Elliot!

    If I var_dump($field['name']); it just spits out the field name from when I set up the field string 'elevation' (length=9). Not sure if I did something wrong.

    From the field itself the elevation field ID value is acf-field-field_51e5de8ec13b9_0_field_51e5e09bc13bb_0_field_51eeec9096476 and the adjacent Home selector field ID is acf-field-field_51e5de8ec13b9_0_field_51e5e09bc13bb_0_field_51ef0daeb0698 – Not sure if those help.

    On a side note, before I posted here, I tried messing with some custom fields. I ended up creating a new field just for my elevations, and selecting the post type when I set up the field.

    I started with the post_object field and modified it a little to closer fit my needs. The largest major modification was how the field choices were loaded. Using the conventions already built into the field class I was able to load the elevations grouped by their parent Home.

    Here is that code:

    $elevations = get_field('elevations_repeater', $p->ID);
    if ($elevations) {
    	foreach ($elevations as $el) {
    		$elName = $el['elevation_name'];
    		$field['choices'][ $title ][ $elName ] =  $elName;

    Then all I had to do was change how the value was saved to the database as just the elevation name, so for the time being I have a workaround, but ultimately I’d prefer the functionality I am talking about.



  • I did something like this to populate a select field.
    add_filter('acf/load_field/key=field_59969e19e1e31', 'my_acf_load_field');
    function my_acf_load_field( $field ){
        $field['choices'] = array();
        $field['choices'][''] = '';
        $titles = array();
    	$args = array(
            'post_type' => array('some_post_type'),
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'orderby' => 'title',
            'order' => 'ASC',
        $clients = get_posts( $args );
    	foreach ( $clients as $client ) {
    		$choices[] = $client->ID;
            $titles[] = $client->post_title;
        if( is_array($choices) ){
    		foreach (array_combine($choices, $titles) as $choice => $title) {
                $field['choices'][ $choice ] = $title;
        return $field;
Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Populate select field with values from repeater field’ is closed to new replies.