Home › Forums › Bug Reports › Programmatically inserting field with update_field() does not at _field_key meta › Reply To: Programmatically inserting field with update_field() does not at _field_key meta
I cannot honestly say without more understanding of the entire code that is being executed when the post is saved. Looking at the functions you’re calling I’m assuming that it would likely take me hours to get this level of understanding. Things can get quite complicated.
Here is a simple example. You submit a post, on the front end 2 things happens. First is that acf runs the acf/pre_save_post hook that gets the post ID. Next the post is saved and following that acf saves the fields, acf/save_post runs.
If you call get_field() during this process ACF will not know what the post ID is that it’s supposed to get the field for because it does not know the post ID. You must supply the correct post ID. The global post ID, if any, will be the post ID where the acf form appears. This is why acf passes the post ID to all acf/save_post actions. You need to pass this ID to any function that will need to get a value from the post that was just saved.
Then there is the issue of calling wp_update_post() inside of an acf/save_post action. What happes is that you cause acf/save_post to run again because this action is tied to WP’s updating of any post. Usually this causes an infinite loop.
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!
📣 “ACF Chat Fridays”— Advanced Custom Fields (@wp_acf) January 31, 2023
The ACF team holds their first open office hours this Friday! Come and talk ACF, and ask questions about building sites with the plugin.
We’d love to see you there!
📆 Friday 3rd Feb - 3pm UTC
👉 Register here - https://t.co/3UtvQbE4CU pic.twitter.com/oTwW9K1XQ0
© 2023 Advanced Custom Fields.