Support

Account

Home Forums Add-ons Repeater Field Dynamically Load Select Options from 2 Different Fields

Unread

Dynamically Load Select Options from 2 Different Fields

  • Hi – I’m trying to set up a select field that pulls the options from 2 different fields inside a repeater.

    Specifically, when a user sets up a Bout, they will select a fighter for the red corner and the blue corner. After the match is over they are able to select the “winner”. I want the winner select field to pull in the selections from the red corner and blue corner fields (so it would pull in 2 options, but from different fields). I tried editing this sample code, but not even the red corner is showing! How to I get it to load the 2 selections? Thank you in advance for any help!

    Fields:
    Repeater field name: bouts
    Red Corner field name: red_corner
    Blue Corner field name: blue_corner
    Winner select field name: winner

    Here’s the code so far in the functions file:

    
    function acf_load_winner_field_choices( $field ) {
    	
    	// reset choices
    	$field['choices'] = array();
    	
    	// get the value from bouts without any formatting
    	$choices = get_field('red_corner', 'bouts', false);
    	
    	// explode the value so that each line is a new array piece
    	$choices = explode("\n", $choices);
    
    	// remove any unwanted white space
    	$choices = array_map('trim', $choices);
    
    	// loop through array and add to field 'choices'
    	if( is_array($choices) ) {
    		
    		foreach( $choices as $choice ) {
    			
    			$field['choices'][ $choice ] = $choice;
    			
    		}
    		
    	}
    	
    
    	// return the field
    	return $field;
    }
    
    add_filter('acf/load_field/name=winner', 'acf_load_winner_field_choices');
    
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.