Dynamically Populate a Select Field’s Choices

Last updated Jul 31, 2023

Overview

Need to dynamically populate a field’s choices? This tutorial shows how to take a value saved to an options page and use it to override the choices array for a Select field.

The ‘choices’ array attribute is also used with the Radio Button and Checkbox fields. Most of this tutorial applies to any of these fields, but the AJAX example can only be used with the Select field.

Requirements

This tutorial requires the use of the acf/load_field filter, in conjunction with the name modifier to apply the filter to all fields of a specific name. The name attribute is how the field is stored and referred to in the WordPress database.

Populating Choices From a Text Area Field

In this example, we will load a Text Area field called my_select_values from an options page, and use the text on each line as an option for the Select field’s choices. The Select field’s name in this example is color.

functions.php

function acf_load_color_field_choices( $field ) {
    
    // Reset choices
    $field['choices'] = array();
    
    
    // Get the Text Area values from the options page without any formatting
    $choices = get_field('my_select_values', 'option', false);

    
    // Explode the value so each line is a new element in the array
    $choices = explode("\n", $choices);

    
    // Remove unwanted white space
    $choices = array_map('trim', $choices);

    
    // Loop through the 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=color', 'acf_load_color_field_choices');

 

Populating Choices From a Repeater Field

In this example, we will load a Repeater field called my_select_values from the options page, and use the subfields value and label as the options for the Select field’s choices.

functions.php

function acf_load_color_field_choices( $field ) {
    
    // Reset choices
    $field['choices'] = array();

    // Check to see if Repeater has rows of data to loop over
    if( have_rows('my_select_values', 'option') ) {
        
        // Execute repeatedly as long as the below statement is true
        while( have_rows('my_select_values', 'option') ) {
            
            // Return an array with all values after the loop is complete
            the_row();
            
            
            // Variables
            $value = get_sub_field('value');
            $label = get_sub_field('label');

            
            // Append to choices
            $field['choices'][ $value ] = $label;
            
        }
        
    }


    // Return the field
    return $field;
    
}

add_filter('acf/load_field/name=color', 'acf_load_color_field_choices');

Populating a Select Field With AJAX

Unlike the Radio Button and Checkbox fields, the Select field can be populated via AJAX. This can help speed up page load times when using the acf/load_field filter to populate the Select field’s choices.

function filter_field( array $field ) : array {

 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {

   $field['choices'] = time_consuming_call_to_get_choices();

 }

 return $field;

}

add_filter( "acf/load_field/key=$key", 'filter_field', 10, 1 );