Support

Account

Home Forums Backend Issues (wp-admin) Updating ACF Data in DB

Solved

Updating ACF Data in DB

  • Hello,

    I appreciate this may be a fairly wide topic but I would really appreciate a basic understanding of what’s going on!

    If I use a form (such as Gravity Forms Advanced Post Creation form) to add a post, everything appears to work correctly. However, ACF fields – like dates – do not work correctly until the post is saved again manually. Something about saving the post manually appears to update the stored data.

    There is clearly a process which goes on when a post is saved in the backend which does not happen when a post is added via a form.

    Presumably I need to update_post_meta somehow after the form has been submitted – but I’m not entirely sure where to start. Cannot anyone please offer me some pointers?

    Thanks

  • When ACF saves values it creates 2 entries in the DB, one of these is the meta_key and the value of the field. The second is a field key reference. This is saved under the meta_key of “_{$field_name}”. Without this field key reference existing the field will exhibit the behavior you are describing.

    If the plugin you are using offers an add on for ACF then you will need to use that.

    If a plugin does not offer an add on that will work with ACF then you will need to do this work yourself. In these cases it is better to not have the form attempt to save the values direction to ACF fields. You should instead use a different field name for the other form and then create an action/filter on whatever hook the other plugin provides when a form is submitted and then use update_field() (see the note about using field keys) so that the field data is inserted correctly.

  • Hi, John –

    Thank you for this explanation. Your support is much appreciated. As things stand I have a small snippet which runs in the background as the form is submitted:

    add_action('gform_advancedpostcreation_post_after_creation_7', 'gf_calendar_date', 10, 4);
    function gf_calendar_date($post_id, $feed, $entry, $form) {
      update_post_meta( $post_id, 'calendar_date', $entry['16'] );	
    } 

    Should this be changed to this…

    add_action('gform_advancedpostcreation_post_after_creation_7', 'gf_calendar_date', 10, 4);
    function gf_calendar_date($post_id, $feed, $entry, $form) {
      update_post_meta( $post_id, 'calendar_date', $entry['16'] );
      update_post_meta( $post_id, '_calendar_date', 'field_63d65a709c89a' );	
    } 

    Thanks again.

  • you can do that or you can just do this

    
    update_field('field_63d65a709c89a', $entry['16'], $post_id).
    

    Something your should also with date fields. ACF does not store proper dates in the DB. ACF stores dates in the format “Ymd” (example: 20230306). You will need to make sure that the value is in this format before inserting into the acf field.

  • Brilliant

    Thank you.

    🙂

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

You must be logged in to reply to this topic.