Home Forums General Issues Add content to ACF Field when post is created not just when saved/updated


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,

    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?

  • Hi John,

    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.

  • This reply has been marked as private.
  • 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 get_sub_field()

    // 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.

    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

    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 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.

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

The topic ‘Add content to ACF Field when post is created not just when saved/updated’ is closed to new replies.