Support

Account

Home Forums General Issues Preload a field group with a json

Solved

Preload a field group with a json

  • Hello,

    I have an empty field group with two fields in ACF that I want to add data to. I have it stored in a json file at the moment and I was wondering if it’s possible to load that data into the field group.

  • Please supply more information.

    You are trying to load this field group to be used on the site?

    You want to add fields to this field group?

    You want to add these fields to another field group?

  • Hi John,

    Thank you for helping me!

    Yes, I’m trying to load the field group for use on my site. I’m not looking to add fields nor add these fields to another field group.

  • See the “Loading Explained” section of this page: Local JSON

  • Hi John,

    I took a look at the link and I’m a bit confused. I don’t see a way where I can dump data into a field group. In my field group, I have an id and name and I want to prefill this field group with data from a json.

  • I thought you wanted to load a field group, not populate fields with data.

    What type of fields? What are the fields associated with? A post? something else? When do you want to populate the fields? when a post/whatever is edited? Some other time?

    There are plenty of topics on the forums that cover populating fields with content but you’re going to need to give specifics, a more thorough explanation of what you want to do.

  • Hi John,

    I have 2 text fields in my field group. The field group is associated with user role at the moment, but that might change in the future to a admin menu page. If possible, I’d like to run a manual upload of the data at startup of the wordpress server since I just need to grab the data from this field group.

    The main purpose of this field group is to keep data for a table to show which ids and names are related.

  • You lost me here

    The field group is associated with user role

    Do you mean a user? ACF cannot be associated with a “Role”, unless you mean that the field group is only shown on the user edit page when the logged in user has a specific role.

  • In any case,

    You can alter a value using an acf/load_value hook. In your filter you would test to see if there is already a value and if not then you can substitute some other value.

    If you want the substituted value to be saved then you would use the update_field() function to do that inside of your acf/load_value filter.

  • Hi, sorry for the late reply. For the role bit, I was referring to the field group’s location rules. I’m assuming location rules defines who/where can access the field group.

    Also, is there a function call to grab all the values in a field group in one go? So if I have two different fields in a field group, I can get an array of said values. Will this list be the same each time I call it as well?

  • The user role location defines if the field group will be seen by the person that is currently logged in, not what role the user being edited is. There is no location rule for the latter.

    get_fields() gets all values for the current post/term/user

  • Ah, I see. I have another question. If I want to load the field group such that a specific text is associated to a certain array, do I use the load value hook?

  • I’m not sure I understand the question. A text field cannot hold an array. In fact, no ACF field can hold an array except those that are designed to do so. Relationship type fields, select and checkbox fields.

  • Also, looking back at my statement about the user role location, I was incorrect, that this based on the user role being edited.

  • Is it possible to have instead of an array a long string with commas in the field?

  • My end goal is to basically store data with a format of “id” to “list” in one field group. Do you know if this possible with acf?

  • I have had luck in the past storing JSON strings in text area fields.

    Is it necessary to use an ACF field? Is this something that needs to be editable? If not then why not use a standard WP meta value. You can save store and retrieve array values using update_post_meta() and get_post_meta().

  • Yes, one requirement is editable fields which is why acf was chosen to host the data.

  • Well, editing a JSON string is not going to be easily usable. Have you looked at using a repeater it will return an array. It may not store it as you want but it would be editable and you could build your array from it. A group field also returns data as an array but it limited to one row.

  • I haven’t though of using a repeater so I’ll look into that. I have one last question. If I need to update an existing value in the field how do I go about it? Let’s say I have 5 different jsons in the field and I need to update the second one.

  • You would have to decode it into an array, update the array values of the array and then json encode it again before updating. There would really be no way of editing it directly in the ACF field because even the slightest error in the json text would invalidate it and return nothing when decoded.

  • I see. I’ll keep that in mind, thank you.

    So just to make sure, if I want to save data to the field directly, do I use the update_field function? I’m trying to implement a save function within an admin page so I don’t have access to normal posts and such. This is all done in a php file.

  • Is there a particular way I need to use update_field when sending a json? I converted my array to json using json_encode and directly fed it to update_field with the field key, but I’m getting false.

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

You must be logged in to reply to this topic.