Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A function for determining an ACF field's sub field key based on a field name. For more info see https://philkurth.com.au/tips/get-an-acf-sub-field-key-by-field-name/
<?php
/**
* Locates an ACF sub-field by field name and returns the sub-field's key.
*
* This is particularly useful if you need to construct a data array for programmatic field
* update where a complex field is in use (e.g; repeater, group, flexi).
*
* @param string $sub_field_name The sub field name we need a key for.
* @param array $field The ACF field array.
*
* @return string The sub-field's field key or an empty string where the sub-field is not found
*/
function get_acf_sub_field_key_by_field_name( $sub_field_name, array $field ) {
$sub_fields = acf_get_fields( $field );
foreach ( $sub_fields as $sub_field ) {
if ( $sub_field['name'] === $sub_field_name ) {
return $sub_field['key'];
}
}
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.