Home Forums Gutenberg Getting ACF block field keys via API? Reply To: Getting ACF block field keys via API?

  • I can’t give you any specific advice.

    Getting the field keys is not going to be an easy task.

    ACF field groups are posts of the type “acf-field-group” the “post_name” value is the group key. “post_content” holds the settings of the field group.

    ACF fields are posts of the type “acf-field”. These are child posts of either the group post or another field post for sub fields. The “post_name” is the field key. “post_content” holds most of the field settings. Some settings are in the _postmeta for the field post.

    Anyway, that’s how ACF stores information.

    Getting that information.

    There is an internal, undocumented function in ACF: acf_get_field_groups() and you will find it in /includes/acf-field-group-functions.php. This function takes one argument that is an array of “filters”. These filters will get the field groups according to the location rules. For example a filter value of array('post_id' => $post_id) would get all of the field groups that would appear when editing a specific post ID. I do not know all of the possible settings for this but you should be able to get field groups with location rules for a specific “block” and you’re going to have to dig through the ACF code to determine what to use for the filters to do this.

    Once you do the above then there is another undocumented function acf_get_fields(). This in in the file /includes/acf-field-functions.php. This takes one argument $parent. This can be the field group key. This will get a nested array of fields that will look like the array for the field group that you’d get if you exported the field group to PHP.

    The above is the process that ACF goes through when showing the field groups in the WP admin.

    To get the field key of a field you would then have to loop over the fields, and sub fields if you have them (recursive function) to get the field information. Here you would still need something to search for to find the field in question, for example the field name, in order to get the field key. In the end something would still need to be hard coded, in this case the field name and or parent field name if looking for a sub field. In the case of sub fields you would also need to branch your recursive function depending on why type of parent field you are looking at.