Home › Forums › Backend Issues (wp-admin) › get_field_objects() empty till Save Draft is clicked
So I’ve seen this happen twice now with two different plugins that put data into ACF fields. One was with a database import plugin and now again with GF+Custom Post Type.
Basically, what is happening, User submits form which creates a post and form fields are mapped to ACF fields. That is working fine. I open up the post edit screen, everything is mapped, even the checkboxes with some coding, no problem.
But if I try to see the field objects for that post, var_dump( get_field_objects( $ID ) ) it is blank until I click Save Draft, or Publish, or something. So it is like the data is in there, but ACF doesn’t really know it, even though, it does seem to know it.
The reason why I need to get the field objects list is that I need to allow the User to edit the post data ( using Gravity Forms Post Update plugin ) and I need to be able to map the ACF serialized checkbox array back out to the checkboxes on the form. I can do this if I hardcode the field names into the code, but I would much rather be able to query the field objects and figure out which ones are checkboxes, and then run my mapping code ( ACF checkbox to GF checkbox ). This is dynamic and better for the user.
More than likely the plugin is not saving the information that ACF needs to map the fields. The pluign you’re using is probably inserting the postmeta for the field correctly, but not the field that tells ACF how to treat that data.
For every postmeta field ACF has an accompanying field.
Lets say that your field name is “my_field” and the ACF field ID of that field is something like “field_123456789abcdef”
ACF will save another postmeta field named “_my_field” with a value of “field_123456789abcdef”
This second field is not saved until you actually edit and save the post. The author of the plugin you’re using would need to take this into account.
Rather than using the acf function get_field(), or the_field(), you can use the standard WP get_post_meta() function to get the value you’re looking for.
Hi Hube2 – Thanks again for answering my post.
Unfortunately the issue is that I need the user to be able to edit their submitted post before any admin has the chance to come in click Save Draft, etc. So with that in mind, I have to be able to get the checkbox data out of ACF and back into GF so the user can edit checkboxes.
Now I can do this just fine if I hard code the field names into the php, but what I would like to do is use the get_field_objects() to test if an object is a checkbox, and if it is, get the name of the field so it can be mapped to the corresponding GF field and then update the data so the user can edit.
I do understand your explanation above, so, is there a way to trigger the Save Draft (I ask because I’ve not found the right hook yet) so that I can trigger that in the final GF hook. Basically, click Save Draft via php. This might not be possible since the Save Draft is a form that updates the post and post_meta, but hopefully there is a way.
Otherwise, yes, I could use get_post_meta() but then I’m counting on those meta field names to tell me what type of field the meta_field is. But maybe I just need to make another meta_field that maps everything out. Which gets back to my suggestion for a plugin to make these mappings. Seems like a fun project, but not in line with what I’m working on now. Guess I’ll just hardcode the field names in for the time being.
I don’t think there’s a way to trigger the save outside of actually saving the post to get ACF to do it’s thing.
If your looking to get a list of fields, if you know the group_id for the field group in question you can use:
That will return a the list of fields, then you could loop through that list of fields to get the information for each field type and get the data based on that.
EDIT: I mean the group key, will look something like “group_123456789abcfef”
Thanks Hude2 – that is a good suggestion. I’ll give that a shot, but at least right now I have it working with the fields hardcoded into the code.
You must be logged in to reply to this topic.
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 demoed ACF 6.1 Beta during the most recent session of ACF Chat Fridays, highlighting the new ability to regenerate and clear labels, setting the Admin Menu Parent as a slug, and more. Catch the video replay in our latest summary. https://t.co/rHEpPVas64 pic.twitter.com/hB1XKTexXi— Advanced Custom Fields (@wp_acf) March 23, 2023
© 2023 Advanced Custom Fields.