Advanced Post Creation Form and Serialized Data

Please forgive my poor understanding of the principals involved. I am learning as I go…!

I believe I have successfully solved a problem I was having with the Advanced Post Creation Form - but I don’t know if I’ve done it right.

My form has an ACF relationship field. The field data was designed to be displayed using a simple function. However, when viewing the post directly after submitting the form, the function would throw an error. I discovered it was necessary to open the post in the back end and re-save it. The function would then suddenly start working.

I spent many long hours studying the way the data was stored in the DB - both after it was first submitted from the form and then after the post was re-saved. In particular, I noticed how my ACF related field data would be stored - first as a Post ID, eg:


…and then after re-saving, if would be serialized, like this…


I have no idea why this should be the case. I only know that my function will only work when the data is serialized.

So, I havd ro find a way to serialize the data using the “gform_advancedpostcreation_post_after_creation” function. I founf an example on the GF support pages and cannabalised it for my purposes. This is what I came up with:

add_action( 'gform_advancedpostcreation_post_after_creation_1', 'apc_serialize_checkboxes', 10, 4 );
function apc_serialize_checkboxes( $post_id, $feed, $entry, $form ) {
    $field_id = 16;
    // Get field object.
    $field = GFAPI::get_field( $form, $field_id );
        $checked = $field->get_value_export( $entry );
        $values = explode( ', ', $checked );
    update_post_meta( $post_id, 'custom-field-name', $values );

To my great surprise, this actually works!

Am I doing this right? Is there a better way? Why doesn’t GF store the data serialized in the first place?

Any info would be very welcome.