Support

Account

Home Forums General Issues Correct Way to Create a New ACF Post

Solved

Correct Way to Create a New ACF Post

  • I am using Formidable Pro for a front end form to manage a Custom Post Type.

    When the user submits a form, I am hooking it and creating a Custom Post Type instead of a Formidable Form Entry.

    I then gather all the form data and I use wp_insert_post() to create the new post with the form data.

    I store all the form data in post meta using the “meta_input” array.

    While that works, and I can access the post meta from a back-end ACF form, the ACF specific post_meta is not created.

    Does it matter? Is there a better way to create a new post with post meta that includes the ACF data?

  • It depends on the fields. For simple text fields it doesn’t matter much. This includes anything that is stored as a text value. But for fields that are stored as serialized arrays or for things like image fields that store image ID this can be a problem.

    You should use update_field() in ACF using the field_keys for the fields. Either that or add the ACF meta values manually. I’d go with using update_field() https://www.advancedcustomfields.com/resources/update_field/

  • The problem with using update_field() is that according to the WordPress Codex, calls to update post meta after wp_insert_post() may fail if the post is still in memory (cached).

    I did try that and it worked most of the time, but occasionally the post meta was not updated.

    An “acf_insert_post()” function would be fantastic. Or a function that fixes the meta_input() array by adding the appropriate ACF info.

    It looks like I might be able to construct something using the “get_sub_field_object()” function and passing the field name.

  • I’m not sure about caching, but I’ve not had a problem in the past so I’m not sure about this. I don’t see ACF adding an function to insert a post. You will need to alter this meta_input() array yourself, which is not a process that’s easily described if you’re using repeaters. Every ACF field that’s inserted should have the field key reference in the database, without it you could have issues displaying the information on the site depending on the type of field.

    Each acf field has two entries in the database

    
    meta_key       | meta_value
    -----------------------------
    $field_name    | $field value
    _{$field_name} | $field_key
    

    Repeater field names are constructed.

    
    // repeater
    {$parent_field}_{$row}_{$sub_field}
    // nested repeater
    {$parent_field}_{$row}_{$sub_field}_{$row}_{$sub_field}
    
  • I found an article on flushing the WP cache to write the post_data to the DB so that I can call the ACF functions.

  • I’m not sure I understand the caching issue. I’ve used insert_post() followed by both update_post_meta() and update_field() when using acf_form() and I’ve never seen an issue, at least not that I can remember, of the meta not being updated. Where is the page in the codex that this is explained, because I’m interested. Maybe I’ve read it and forgot.

  • I didn’t find it in the codex. I caught a post on a blog about caching issues when using a third-party cache plugin.

    In plain-vanilla WordPress just calling update_field() after wp_insert_post works perfectly. However, we are using WP Super Cache and I’ve seen update_field() fail because the post ID doesn’t exist in the database due to the data being cached in memory.

    I don’t have the article on my work computer, I was working on my laptop over the weekend and it’s there.

    In any case, WP Super Cache has a filter for database reads and writes and hooking that will flush the memory cache.

    Code works fine now.

  • That makes sense actually, since it’s a front end form.

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

The topic ‘Correct Way to Create a New ACF Post’ is closed to new replies.