Home Forums Add-ons Repeater Field Populate choices from other fields without saving post


Populate choices from other fields without saving post

  • I’m using the following code to populate choices for a subfield in based on values of two other fields (I need to allow the editor to type in the fields person_1 and person_2 then use those two values as the choices in a subfield for the same post.) This works to populate those choices, but only if you add the two values, then save before you move onto the subfield. Is there a way to dynamically add the choices from other fields without having to save the post first? Thanks!

    // Load Chat / Interview Names to field
    function RS_acf_load_field( $field )
    	// reset choices
    	$field['choices'] = array();
    	// get the choice values from other fields
    	$choice1 = get_field('person_1');
    	$choice2 = get_field('person_2');
    	$choices = "$choice1;$choice2";
    	// explode the value so that each line is a new array piece
    	$choices = explode(";", $choices);
    	// loop through array and add to field 'choices'
    	if( is_array($choices) )
    		foreach( $choices as $choice )
    			$field['choices'][ $choice ] = $choice;
        // Important: return the field
        return $field;
    add_filter('acf/load_field/name=interview_name', 'RS_acf_load_field');
    • Elliot

    • September 11, 2013 at 2:11 pm

    Hi @refreshingdesign

    Good question.
    To modify the DOM without reloading the page, you will need to write some custom jQuery on the edit screen.

    You could either use an AJAX call to allow PHP to return the choices, or just look at the text inputs to generate the choices.

    To add a script tag to the edit screen, use the acf/input/admin_head action.
    You find some examples of this action on the docs (actions) page.


  • Thanks for the lead, Elliot! I have to believe that someone else has run across this and come up with a solution… Any way I could get some direction on what script to use in the function? Thanks so much for your help!

    • Elliot

    • September 12, 2013 at 6:17 pm

    Hi @refreshingdesign

    Sorry mate, it would be almost faster for me to write the code than to explain how it would work.

    That said, that’s far beyond this support forum.
    Thanks for understanding and good luck with the script.


Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.