Home › Forums › General Issues › Add content to ACF Field when post is created not just when saved/updated
I am trying to populate some ACF text fields with calculations when a post is created. Using the acf/save_post action, but it seems to only work when i update or save a post. How do I make it run when the post is actually created?
You might try using the load_field hook, http://www.advancedcustomfields.com/resources/acfload_field/
You could test to see if it’s a new post and if not then set some default values based on other default values. I’m not exactly sure what you’re looking for though. If it’s a new post then wouldn’t you just use default values for these fields? Are you talking about when a new post page is first loaded or when you first publish a post?
I mean I want the fields to be populated when the post is first published.
With the acf/save_post action I have to physically press the save/update button in the published post for the calculated values to be added to the custom fields.
The acf/save_post hook is fired whenever a post is saved, whether that is when updating an existing post of publishing a new post.
The strange thing is that my title gets created whenever the post is published, but the calculations don’t get populated. I might be missing something in the code.
Try changing all of your acf function calls to include the post ID, but not this is not necessary for
// change this if( have_rows('bag_contents', $post_id) ): while ( have_rows('bag_contents', $post_id) ) :
// don't change this $produce = get_sub_field('produce');
// change this update_field('total_servings_per_week', $per_week, $post_id);
And I think you should only need to add your action once at priority 20
I made the updates. But the problem still remains. I forgot to mention, this form is a front end form. Do you think that has something to do with it?
I noticed that fields update like they should when I create a post in wordpress backend. Could that be the issue?
You’re creating a new post on the front end, that’s a different story. You might need to have a pre save post filter. http://www.advancedcustomfields.com/resources/acf-pre_save_post/
What do your arguments for acf_form() look like for creating the new post? Are you including arguments for the new post?
It doesn’t have any arguments.
Take a look at the Creating a New Post section of the acf_form() documentation http://www.advancedcustomfields.com/resources/acf_form/
My guess is that the values are being added to whatever post or page your front end form for the new post form is on.
Turns out I was referencing the wrong template. My acf_form() did have the correct arguments.
I solved the problem by using field keys instead of field names and adding $post_id to the update_field function.
E.g. update_field(‘field_55760114e5887’, $per_week, $post_id);
Thanks a lot for helping me troubleshoot the problem though!
That makes sense. Sometimes I forget about the basics. Yes, when adding new posts you must use the field key in update_field(), which is explained here http://www.advancedcustomfields.com/resources/update_field/. Glad you got it worked out.
One last thing. How would I “trigger” all the posts to be updated? Because otherwise I’ll have to physically press update on every post, whether it be on the front-end or the backend.
I don’t think you can trigger all posts to be updated, at least not easily.
I suppose, if you built a function that got all posts in you post type and called
do_action('save_post', $post_id); or
do_action('acf/save_post', $post_id); that might cause it, but I don’t know what other side effects there may be or even if it will work.
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’re hard at work on ACF 6.1, and Beta 1 is now available 🚀— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.