Populating fields dynamically in ACF is simple using the acf/load_field hook. An example:
//Populate the Select field field_5587fccf24f38 with dynamic values
add_filter('acf/load_field/key=field_5587fccf24f38', function($field) {
//These can be dynamically generated using any PHP code
$field['choices']['one'] = 'Choice one';
$field['choices']['two'] = 'Choice two';
return $field;
});
But if you also use ACF Local JSON, you’ll notice that your dynamic field values get exported whenever you re-resave one of your field groups. Not ideal.
Using dynamic values with ACF Local JSON
We need a way to remove the dynamic values just before they are exported to Local JSON. Luckily, there’s an undocumented filter called acf/prepare_field_for_export
that will let us do this! Example:
//Don't export dynamic values via Local JSON
add_filter('acf/prepare_field_for_export', function($field) {
//If we're at the correct field
if(isset($field['key']) && $field['key'] === 'field_5587fccf24f38') {
//Blank out the select options with an empty array
$field['choices'] = array();
}
return $field;
});
Updated 2016-01-15
There was another filter called acf/prepare_fields_for_export
(note fields
instead of field
). This filter was removed in ACF 5.3.3.