Support

Account

Home Forums General Issues Local Json in plugins + acf_get_field_group = can't find fields or group

Solved

Local Json in plugins + acf_get_field_group = can't find fields or group

  • Hey there,

    I am really trying to nail a GIT based ACF workflow but keep getting caught up on accessing the field keys etc. To that end I have been packaging up json files that represent a custom post type inside my plugins and adding the plugin path using acf/settings/load_json. This works fine for displaying the fields in the admin section etc but not if I want to do anything programatically.

    I have a helper function that I have been using that allows me to pass an array of keys and values and they will save the values back to the custom fields. This was to get around having to always lookup the field keys etc.

    function save_fields($group, $post_id, $values) {
        $fields = acf_get_fields($group);
        foreach( $values as $key => $value ) {
            $field = wp_list_filter( $fields, array('name' => $key ) );
            if (count($field) > 0)
                update_field($field[0]['key'], $value, $post_id);
        }
    }
    
    save_fields('group_559a17278c508', 123, array(
       'tagline' => 'Test Tagline',
        'other_field_name' => 'Other Value'
    ));
    

    The issue I am having is acf_get_field_group() and acf_get_fields() etc do not work with the Local Json files I have stored in my plugin. Has anyone had any luck with using Json files and successfully retrieving the keys dynamically etc?

    What are other people doing in regards to packaging up their JSON in GIT etc and having it play nicely with the built in ACF field functions?

  • You could be running int an ACF caching issue, I’ve had trouble with it in the past while working on plugins. When you get a field group ACF sets a cache value. Once this is done you basically can’t alter he field group, well you can, but nothing happens. After you get a value you sometimes need to clear a cache before you can make modifications to te field group you need to clear that cahce.

    YOu’ll need to did around in the ACF code to figure out that cache value you need to clear.

  • Thanks for your reply,

    The issue I am having specifically is acf_get_field_group() and acf_get_fields() etc don’t “see” my field group stored as JSON within my plugin folder.

    ACF reads them OK and displays the fields in the admin section but I can’t load their fields using the above calls.

  • so if you output the field group after you get it you see nothing?

    
    $fields = acf_get_fields($group);
    echo '<pre>'; print_r($fields); echo '</pre>';
    
  • That’s right,

    And acf_get_field_groups my JSON group isn’t listed.

  • What happens if you alter your code to this:

    
    $groups = acf_get_field_groups();
    echo '<pre>'; print_r($groups); echo '</pre>';
    
    save_fields('group_559a17278c508', 123, array(
       'tagline' => 'Test Tagline',
        'other_field_name' => 'Other Value'
    ));
    

    Do you see any field groups? If you do are group_559a17278c508 listed?

  • Somehow I missed the part about your group not being listed.

    I’ve run into issues with the ACF caching of field groups in the paste. Basically, if you add field groups or fields after they have been added to the changes you make will have no effect.

    The way that I resolved this was to have my code clear the cached value that my code caused to be set. For example I used acf_get_field_groups(). This caused all the field groups to be cached. Then I added a new field group and the new field group never appeared. What I needed to do was to clear the cached values after I called acf_get_field_groups().

    My suggestion is to look at the functions in the acf code that you’re calling and see if ACF is caching any values and if it is to clear those cached values right after you call the acf function.

    Hope that helps.

  • Argh, thanks for your help, you put me on the right track. acf_get_field_groups doesn’t load local groups but the acf_local class does run a filter which looks for these groups. I was calling acf_get_field_groups directly in my functions.php (while testing) and on init action but that filter hadn’t run yet.

    Calling acf_get_field_groups later in the action pipeline (wp / wp_loaded) etc showed the fields.

    Thanks for your help.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Local Json in plugins + acf_get_field_group = can't find fields or group’ is closed to new replies.