There\'s a few ways to achieve this but here\'s one which supports an unknown depth of children, for example where you have nested groups within groups.
Given the field key "field_{some_hash}" of the field, this function will retrieve the field, and its parent most field, and so on until we reach the top of the field tree.
function custom_acf_get_field_name_by_key( $key ) {
$field = acf_maybe_get_field( $key );
if ( empty( $field ) || ! isset( $field[\'parent\'], $field[\'name\'] ) ) {
return $field;
}
$ancestors = array();
while ( ! empty( $field[\'parent\'] ) && ! in_array( $field[\'name\'], $ancestors ) ) {
$parent = acf_get_field( $field[\'parent\'] );
$ancestors[] = $field[\'name\'];
$field = $parent;
}
$formatted_key = array_reverse( $ancestors );
$formatted_key = implode( \'_\', $formatted_key );
return "_$formatted_key";
}
Assume a case where I have a nested group:
- dev_group_test | field_61ecf308ded3e | group
∟ title | field_61ecf316ded3f | text
∟ location | field_61ecf31cded40 | text
∟ sub_group | field_61ecfa1fe5894 | group
∣
∟ sub_group_title | field_61ecfa2de5895 | text
If I want the full key of sub_group_title
:
$meta_key = custom_acf_get_field_name_by_key( \'field_61ecfa2de5895\' );
// result = _dev_group_test_sub_group_sub_group_title
If I want the full key of location
:
$meta_key = custom_acf_get_field_name_by_key( \'field_61ecf31cded40\' );
// result = _dev_group_test_location
NOTE: there may be more efficient ways to do this