I’m trying to add the acf to an existing submission form of my website. The fields displayed without issues, but there are two problems.
1. It needs to save twice for saving the data to backend.
2. The fields keeps showing the previous entries when I was trying to submit the form again.
Here is the code I added within the form
acf_form_head(); <?php acf_form( array( // acf fields 'field_groups' => array( 'group_600c5a53ec968'), 'form' => false, 'return' => false, ) );?>
Here is the code I added in the function.php
//acf form update //start date $acf_start_date = $_POST['acf']['field_60168af55da39']; update_post_meta($post_id, 'start_date', $acf_start_date ); //end date $acf_end_date = $_POST['acf']['field_600c433404fdc']; update_post_meta($post_id, 'end_date', $acf_end_date ); //coupon $acf_coupon = $_POST['acf']['field_601691bf48dc4']; update_post_meta($post_id, 'coupon', $acf_coupon );
The issue lasted for few weeks already…It would be great if someone can help. Thanks a lot!
1) acf_form_head() must be output before any html, so before get_header() is called and not just before the form. Is this the case.
2) acf_form() must be called inside or the
<form></form> tags of the existing form. Is this what you’ve done?
3) I do not understand why you are attempting to update the fields manually in the second part of your code since ACF will do this for you if you’ve set up the form correctly.
Thank you for the reply.
1 & 2) yes, I did both.
3) I did it because the fields couldn’t update automatically after submission. After I submitted the form, only the data from existing form was being updated.
I have also tried to add
'post_id' => 'new_post' in acf_form(), the data can be updated immediately, but two posts were being created.
I would need to have more information on what you’re trying to add this to.
The only thing that I can think of here is that whatever you’re trying to add the ACF fields to is not processing the submission in a way that allows ACF to work. And if that’s the case then your manually trying to do so may not work either.
Also, if you are trying to update fields manually where data is not already saved then you need to use the ACF function to update the fields and you need to use the field key.
$acf_start_date = $_POST['acf']['field_60168af55da39']; update_field('field_60168af55da39', $acf_start_date, $post_id);
But all these are just guesses.
The existing frontend form is for the members to submit a new post in website. As the post is about is discount or coupon details, I wanna use ACF to add few more fields to the existing form, which are deals start date, end date and coupon code.
Do you mean the existing form may not allow ACF to function well?
Didn’t know if this information useful, I tried to add a field of tag by ACF in the function.php, and it works. Does it mean ACF is able to function in the form?
$submission_data = array( 'id' => $post_id, 'title' => $title, 'source' => $source, 'ref_link' => $ref_link, 'description' => $description, 'category_id' => $categories, 'tags' => $_POST['acf']['field_6023f52f7b6f0'], 'list_voting' => $list_voting, 'list_submission' => $list_submission, 'author' => $author_id, 'status' => $status, ); do_action( 'snax_handle_'. $format .'_submission', $submission_data, $request );
I have come up with a thought.
The ACF fields keep showing the previous entries because the existing form don’t have a post id yet until users submitted the form. Therefore, the form needs to save twice in order to get the data from ACF.
If this is the case, is there any way ACF can solve it?
If there is no post ID when ACF is saving the information then it could be saved anywhere. I’m sure ACF is saving the information somewhere, but without it being able to get the post ID then it cannot save. ACF runs on the WP hook “save_post”. This hook passes the post ID and if there is no post ID then ACF attempts to figure out what it is by looking at WP global values. If you’re plugin is somehow bypassing the normal method of adding a post when a form is submitted then ACF is probably using the post ID of whatever page the form appears on instead of the post ID of the post created by that plugin.
If the above is the case what I would do is look into the plugin and figure out is there is a hook in that plugin that runs after it has created the post that hopefully passes the post ID to actions or filters and I would add an action or filter to for that hook and in my filter I would run the acf filter by doing
do_action( 'acf/save_post', $post_id );
Thanks so much for you detailed reply!
I have found the hook finally. I added
do_action( 'acf/save_post', $post_id ); after
$post_id = wp_insert_post( $new_post ); and the fields can be updated in once.
However, another issue still haven’t be solved. When I’m trying to submit another new form, the previous entries shows again. Do you have any idea how can I solve this? I found that the existing form sets the
post_id = 0 before generating a new post ID.
I have no idea. This line should cause acf to run and save values to the correct post ID because it triggers the sequence of events that are needed.
$post_id = wp_insert_post( $new_post );
The fact that was not happening means that you have larger issues that may or may not be associated with that plugin.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2021 Advanced Custom Fields. Subscribe