Support

Account

Home Forums General Issues Local JSON – get_field() returns only using key

Solved

Local JSON – get_field() returns only using key

  • I have a setup where there’s 0 references in the postmeta table regarding ACF which is pretty neat. Much of the content is paired and pulled with custom tables and local JSON. The integration file is pretty straight forward and works thanks to all the great hooks built into this plugin.

    My problem is this:

    get_field( 'first_name', $post->ID );

    I have hooked onto acf/load_value which will return value from my custom tables. At the top of this hook I print the passed $field and it’s empty, like it’s not loading the local JSON. This is probably part of my previous question regarding priming JSON cache now that I Think about it. The field looks exactly like this:

    Array
    (
        [ID] => 0
        [key] => 
        [label] => 
        [name] => first_name
        [prefix] => 
        [type] => 
        [value] => 
        [menu_order] => 0
        [instructions] => 
        [required] => 0
        [id] => 
        [class] => 
        [conditional_logic] => 0
        [parent] => 0
        [wrapper] => Array
            (
                [width] => 
                [class] => 
                [id] => 
            )
    
        [_name] => first_name
        [_prepare] => 0
        [_valid] => 1
    )

    I can also try to call get_field_object( $field['name'], $post_id, false, false ); in the acf/load_value hook but it returns the same result as above.

    If I call the function again using the field key instead of the name:

    get_field( 'field_5b69bd5a6b018', $post->ID );

    The passed field into acf/load_value is now populated as expected from local JSON and I can go about my business with successful results.

    I’ve been trying to backtrack through the get_field() process where specifically it fails and I’m thinking _maybe_ it’s losing it at a function that specifically looks into the post metadata acf_get_metadata() but I can’t be certain.

    I’m 100% open to any ideas or suggestion to figure out why this may be happening.

  • Whenever passed by name it can’t seem to get the field key from acf_get_reference(). It’s got 3 filter hooks though, any suggestions on functions I could use to simply load all my cache all my local fields and/or start searching through my local field names for a 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 );
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.