Support

Account

Home Forums Add-ons Repeater Field Dynamically populating a repeater SELECT field

Helping

Dynamically populating a repeater SELECT field

  • Hi

    I have a repeater field (boats_due) with a sub field SELECT (boat_due) in there. I would like to populate this subfield select (boat_due) with the value of a textarea in an options panel (boat_due_names).

    As the SELECT is a sub field within a repeater field that I need to populate, I am a little lost from the docs how to do this?

    I have tried the below.

    Thanks

    function acf_load_field( $field ) {
    	
    	// if has rows
    	if( have_rows('boats_due', 'option') ) {
            
        // while has rows
         while( have_rows('boats_due', 'option') ) {
    		 
    		 // instantiate row
                the_row();
    		 
    		 // reset boat due
        		$field['boat_due'] = array();
    		 
    		 // vars
                $value = get_sub_field('boat_due');
    	 }
    	}
        
        // get the textarea value from options page without any formatting
        $choices = get_field('boats_due_names', 'option', 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 'boat due'
        if( is_array($choices) ) {
            
            foreach( $choices as $choice ) {
                
                $field['boat_due'][ $choice ] = $choice;
                
            }
            
        }
        
    
        // return the field
        return $field;
        
    }
    
    add_filter('acf/load_field/name=boat_due', 'acf_load_field');
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Dynamically populating a repeater SELECT field’ is closed to new replies.