Home › Forums › General Issues › Get field key by field name › Reply To: Get field key by field name
The “real” answer: go to the Field Group admin page, click the “display field key” button in help dropdown, then copy & paste the key into code.
Maybe closer to the answer you’re looking for:
function acf_field_key($field_name, $post_id = false){
if ( $post_id )
return get_field_reference($field_name, $post_id);
if( !empty($GLOBALS['acf_register_field_group']) ) {
foreach( $GLOBALS['acf_register_field_group'] as $acf ) :
foreach($acf['fields'] as $field) :
if ( $field_name === $field['name'] )
return $field['key'];
endforeach;
endforeach;
}
return $field_name;
}
However, this only works for fields registered with PHP via register_field_group().
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.