Home › Forums › General Issues › Local JSON – get_field() returns only using key › Reply To: Local JSON – get_field() returns only using key
This is the final solution I’ve come up with. I found the function which returns all the local fields, compared them to the field groups I expected in my plugin, then looped through the names, breaking out of the loop on the first match.
I doubt this will work in most setups, duplicate names are a possibility, you could probably check against the 3rd parameters $post_id
for a post type, then the post type against $local->groups and each groups location parameters. #worksForMe
/**
* Get local field by name
*
* @param String $reference - Field unique ACF key
* @param String $field_name - Field nice name/slug
*
* @return String $reference
*/
function get_field_by_name( $reference, $field_name ) {
$local_fields = acf_local(); // Grab all local fields
// Ensure we have local fields to work with
// Ensure that reference is empty
if( empty( $local_fields ) || ! empty( $reference ) ) {
return $reference;
}
// Loop through groups
foreach( $local_fields->fields as $key => $field_arr ) {
/**
* Ensure the field is in our expected groups.
* Removed when not in the context of plugin
* $this->field_group_keys = array( 'group_xyz' ) - Holds array of expected local field groups
*/
// if( ! in_array( $field_arr['parent'], $this->field_group_keys ) ) {
// continue;
// }
// Break the loop on first found match
if( $field_name === $field_arr['name'] ) {
$reference = $key;
break;
}
}
return $reference;
}
add_filter( 'acf/get_field_reference', 'get_field_by_name', 100, 2 );
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.