Title: Help regarding saving as draft
Last modified: November 29, 2023

---

# Help regarding saving as draft

 *  [edselopez](https://wordpress.org/support/users/edselopez/)
 * (@edselopez)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/help-regarding-saving-as-draft/)
 * I tried asking this question to the folks over at Advanced Custom fields, but
   they redirected me to this email, as my use case falls under this plugin plugin.
   Hoping someone can assist with this.
 * We have a workflow where we allow a user to save an ACF form submission as draft,
   before they can proceed further. Here is some code which we are using to save
   to draft:
 *     ```wp-block-code
       function my_acf_button_ajax($field, $post_id){
       // Check if current user has any posts.
       $post_id = getDraftPost();
       if (!empty($post_id)) {
       if( $_POST['acf'] ) {
       update_post_meta( $post_id, 'entry_form', FORM_KEY );
       update_post_meta( $post_id, 'entry_submission_date', date( 'Y-m-d H:i:s' ) );
       acf_update_values( $_POST['acf'], $post_id );
       }
       }
       else {
       $post = [
       'post_status' => 'draft',
       'post_type' => 'af_entry',
       ];
   
       // insert the post
       $post_id = wp_insert_post($post);
       if ($_POST['acf'] && $post_id) {
       update_post_meta( $post_id, 'entry_form', FORM_KEY );
       update_post_meta( $post_id, 'entry_submission_date', date( 'Y-m-d H:i:s' ) );
       acf_update_values($_POST['acf'], $post_id);
       }
       }
       // Set post ID which we will use during submission, and other places.
       wp_send_json_success("Draft has been saved!");
       }
       ```
   
 * Here is the getDraftPost() function, which simply looks for the most recent draft
   for the current logged in user.
 *     ```wp-block-code
       function getDraftPost($status = 'draft') : int {
       global $current_user;
       // Check if current user has any posts.
       $args = [
       'author' => $current_user->ID,
       'orderby' => 'post_date',
       'order' => 'DESC',
       'post_type' => 'af_entry',
       'post_status' => $status,
       ];
   
       $current_user_posts = get_posts( $args );
   
       if (!empty($current_user_posts)) {
       return $current_user_posts[0]->ID;
       }
       return 0;
       }
       ```
   
 * And finally, the code used to populate defaults, if a draft is found.
 *     ```wp-block-code
       function set_draft_values( $field ) {
       $post_id = getDraftPost();
   
       if (!empty($post_id)) {
       $fieldValue = get_post_meta($post_id, $field['name'], TRUE);
       if (!empty($fieldValue)) {
       $field['default_value'] = $fieldValue;
       }
       }
   
       return $field;
       }
       add_filter('acf/load_field', 'set_draft_values');
       ```
   
 * As you can see, we are using the acf/load_field to populate the default values.
   All of this works, but we are noticing that sometimes, values that are saved 
   in drafts for one user, are populated on forms that are being submitted by another
   user altogether. There is no sharing of computers, and this happens completely
   at random for maytbe 1/50 submissions. Is there a better way to therefore populate
   the form with values saved from a draft where this pollution of data doesn’t 
   happen?
 * Few things to note:
    1. We are using the latest version of ACF pro: 6.2.3
    2. We are using Advanced Forms as we have a multi-page layout, where we are displaying
       the ACF fields in tabs.
    3. The form is only accessible to logged in users, so we’ll always have the User
       ID for the current logged in user.
 *  -  This topic was modified 2 years, 6 months ago by [edselopez](https://wordpress.org/support/users/edselopez/).

The topic ‘Help regarding saving as draft’ is closed to new replies.

 * ![](https://ps.w.org/advanced-forms/assets/icon-256x256.png?rev=1894254)
 * [Advanced Forms for ACF](https://wordpress.org/plugins/advanced-forms/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/advanced-forms/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/advanced-forms/)
 * [Active Topics](https://wordpress.org/support/plugin/advanced-forms/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/advanced-forms/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/advanced-forms/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [edselopez](https://wordpress.org/support/users/edselopez/)
 * Last activity: [2 years, 6 months ago](https://wordpress.org/support/topic/help-regarding-saving-as-draft/)
 * Status: not resolved