Home Forums Front-end Issues Need to save post in backend before I can programmatically adjust ACF fields


Need to save post in backend before I can programmatically adjust ACF fields

  • This is what I try to do:

    1. I create a post programmatically, using the WordPress frontend.

    2. I initiate an ACF field, for this post, to an empty string.

    3. I update the same ACF fields using the frontend. That is, I set the value of the string.

    4. I read the value of the ACF field and append characters to the string, then save the field again.

    The above does not work. The value of the ACF field is always returned as empty. After I’ve saved something in the field, I can see, in the WordPress backend, that the save was successful. But, reading it in the frontend returns an empty string, every time.

    Saving the new value of the ACF field only writes the new value to the ACF field the first time I try this. All subsequent times, the write appears successful, but the backend shows that nothing is updated after the first save.

    This starts to work properly when, at least once, the new post is saved using the WordPress backend. After a save in the backend, every subsequent try, in the frontend, reads the current value of the field correctly and updates it correctly.
    It doesn’t matter whether the new post is saved using the WordPress backend directly after creating the post, or at any time after attempting to update the ACF fields.

    The above problem also occurs for relationship fields and does not occur for custom fields that are added outside of ACF.

    How I initiate the ACF fields upon creation of the post appears to make no difference. I have tried each of the three below:

    add_post_meta($newId, "slides_as_string", "");

    update_field("slides_as_string", "", $newId);

    add_post_meta($newId, "slides_as_string", "");
    update_field("slides_as_string", "", $newId);

    The scenario for the above is as follows:

    1. A user creates a new instance of a custom post type in the frontend.
    2. The post is created and the user is redirected to the URL for the new post.
    3. The user might choose to include a bunch of images as part of a slideshow, for this new post. The images of the slideshow are stored in a relationship field.

  • Some good support from ACF later, I learned I have to use the field key, not the field name, to update it. The field key can be shown on the ACF admin pages by tapping ‘Screen options’ at the top of the window, then turning on the relevant options.

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

You must be logged in to reply to this topic.