
A suggestion for documentation…
I spent quite a while figuring out how to populate a select element with the fields from field group. all the code I was able to find online was for older versions than V5 and didn’t work. In the end I got it to work like this. I wonder if this is the right way to do it but at least it works… If this is valid, this could be a worthwhile addition to this page: http://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
function acf_load_select_choices( $field ) {
global $wpdb;
$group_slug = 'Name or slug of the group';
$group_ID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$group_slug' ");
if (empty($group_ID))
$group_ID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$group_slug' ");
$field['choices'] = array();
$fields = acf_get_fields_by_id($group_ID);
// to see what $fields contains, use:
echo '<pre>'.var_dump($fields).'</pre>';
if( $fields ) {
foreach( $fields as $fieldrow ) {
$field['choices'][ $fieldrow['name'] ] = $fieldrow['label'];
}
}
return $field;
}
add_filter('acf/load_field/name=selectfieldname', 'acf_load_select_choices');
Hi @aatospaja,
Thank you for sharing the code, we will surely make arrangements to add this to the ACF resource page, it will surely come in handy to another dev.
Great work!!