Last updated on October 5, 2023

ACF Dynamically Populate Field

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Dynamically populate any ACF field with acf/load_field and acf/load_value.

General steps

To dynamically populate an Advanced Custom Fields (ACF) select field, you can use the acf/load_field filter to modify the field’s settings before they are rendered. It’s also possible to filter the value of a field, using the acf/load_value filter. This allows you to retrieve data from a source, such as a database or an API, and populate a select fields options, or a text fields value, dynamically.

The way to use the acf/load_field filter for example, is by hooking a custom callback function that modifies the fields settings. For example:

function wpsnippets_populate_acf_select_field( $field ) {

    // Check if this is the select field you want to populate
    if ( $field['key'] === 'field_1234567890' ) {

        // Query or retrieve your data from a source
        $data = array(
            'option1' => 'Option 1',
            'option2' => 'Option 2',
            'option3' => 'Option 3',
        );

        // Assign the data to the field choices
        $field['choices'] = $data;
    }

    return $field;
}

add_filter( 'acf/load_field', 'wpsnippets_populate_acf_select_field' );

Filter specific fields only

The acf/load_field filter can be modified to target only specific fields. The modifiers that exist, are:

  • acf/load_field Filters all fields
  • acf/load_field/type={$type} Filters all fields of a specific type
  • acf/load_field/name={$name} Filters all fields with a specific name
  • acf/load_field/key={$key} FIlters all fields with a specific key

Here’s an example of how to use thee modifiers:

// Filter all fields
add_filter('acf/load_field', 'wpsnippets_filter_all_fields');

// Filter all 'select' fields
add_filter('acf/load_field/type=select', 'wpsnippets_filter_select_fields');

// // Filter all fields with the name 'my_field'
add_filter('acf/load_field/name=my_field', 'wpsnippets_filter_custom_fields');

// // Filter the field with the key 'xyz123'
add_filter('acf/load_field/key=field_xyz123', 'wpsnippets_filter_specific_field');

Dynamically Populate ACF Select Field Options

To dynamically populate an ACF select field, we can use the acf/load_field/type=select filter to target all select fields. If you want to target a specific field only, it’s better to use the acf/load_field/name={$name} filter. With the method below, you can also populate the following fields:

  • ACF Checkbox Field
  • ACF Radio Button Field
  • ACF Button Group Field

In the example below, we are going to retrieve 5 posts and populate the select field with them, so that every post becomes an option in an ACF select field.

function wpsnippets_populate_my_select_field( $field ) {

    // Optionally: Remove all options added in the back end
    $field[ 'choices' ] = array();
    
    // Get 5 posts
    $posts = (array) get_posts( array(
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'posts_per_page' => 5,
    ) );

    // Check if posts were found
    if( $posts ) {
        
        // Loop over the posts
        foreach( $posts as $post ) {

            // Define the option's parameters
            $value = $post->ID;
            $label = $post->post_title;

            // Add every post as an option to the select field
            $field[ 'choices' ][ $value ] = $label;
            
        }
        
    }

    // Always return the $field
    return $field;
}

// Only filter the ACF fields with the name 'my_select_field'
add_filter( 'acf/load_field/name=my_select_field', 'wpsnippets_populate_my_select_field' );

Dynamically Populate ACF Text Field Value

To dynamically populate an Advanced Custom Fields (ACF) text field value, you can use the acf/load_value filter to modify the field value before it is displayed. This allows you to populate (for example) an ACF Text field’s value. With the method below, you can populate all ACF fields’ values.

function wpsnippets_populate_acf_text_field( $value, $post_id, $field ) {
    
    // Retrieve the dynamic data
    $dynamic_data = (string) get_the_title( 123 );

    return $dynamic_data;
}

// Only filter the ACF fields with the name 'my_text_field'
add_filter( 'acf/load_value/name=my_text_field', 'wpsnippets_populate_acf_text_field', 10, 3 );

In this example, we retrieve the title of a post with the ID 123, and populate an ACF Text field’s value with the post’s title.

Dynamically Populate ACF Repeater Field

To dynamically populate an Advanced Custom Fields (ACF) repeater field, you can use the same method as described above, but using the acf/load_value/key={$key} filter to modify the repeater field values before they are displayed. Here’s an example of how you can achieve this:

In the example below, we are going to and populate an ACF Repeater field with some random values. You can however retrieve any data and use it to populate an ACF Repeater field’s value.

function wpsnippets_populate_acf_repeater_field( $value, $post_id, $field ) {

    // Query or retrieve your data from a source
    $data = array(
        // First row
        array(
           'field_uvwxyz987654' => 'Value 1' // Sub field 1 → Use field keys
           'field_ghijkl678901' => 'Value 2', // Sub field 2 → Use field keys
        ),
        // Second row
       array(
            'field_uvwxyz987654' => 'Value 3', // Sub field 1 → Use field keys
            'field_ghijkl678901' => 'Value 4', // Sub field 2 → Use field keys
        ),
        // Add more rows as needed
    );

    // Assign the data to the repeater field value
    $value = $data;

    // Always return the $value
    return $value;
}

// Use the 'acf/load_value/key={$key}' filter to target your repeater field
add_filter( 'acf/load_value/key=field_12345abcdef', 'wpsnippets_populate_acf_repeater_field', 10, 3 );
Last updated on October 5, 2023. Originally posted on May 18, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *