Skip to content

Instantly share code, notes, and snippets.

@simonwheatley
Created January 27, 2014 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonwheatley/8645866 to your computer and use it in GitHub Desktop.
Save simonwheatley/8645866 to your computer and use it in GitHub Desktop.
Getting an Advanced Custom Fields field_key from a field_name and field_group.
/**
* Get a field key from a field name. Why isn't this an
* ACF API function?
*
* @param string $field_group The name of the field_group to search
* @param string $field_name The name of the field to find a key for
* @return string|bool The field key if found, else false
*
* @author Simon Wheatley
**/
protected function get_field_key( $field_group, $field_name ) {
$field_definitions = apply_filters('acf/field_group/get_fields', array(), $field_group );
foreach ( $field_definitions as $def )
if ( $field_name == $def[ 'name' ] )
return $def[ 'key' ];
return false;
}
@igelman
Copy link

igelman commented Jul 4, 2014

Hi Simon --
Super handy ... if I could only make it work ;-)

What value does this function take for $field_group? Individual fields have "Field Label" and "Field Name", which I set in ACF's UI. But there's nothing similar for the Field Group.
In ACF, I have a group "My Group". Should I pass "My Group"? "my-group"? Some id value that I need to find in the DB?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment