Hi!
I updated from ACF 4 to ACF Pro 5.6.1 and now I cant get my acf_form() to work. I have a front-end form that saves the input data to a new custom post. It seems like acf/pre_save_post wont trigger like it used to in v4. The post is created in WP but as a regular post and not as the custom post type ‘ansokningar’ that it should. And it’s not working to generate post_title from an acf-field either.
Furthermore I have a function for file upload. The file gets uploaded as it should via the form, but it loses the attachment to the newly created post. It gets uploaded to the media library but unattached to the correct post.
Is the acf/pre_save_post-filter still working in ACF 5 ?
Here’s my full code:
FORM.PHP:
<?php acf_form(array(
'post_id' => 'new_post',
'field_groups' => array( 1176 ),
'updated_message' => __("Thank you for your application", 'acf'),
'submit_value' => 'Submit application',
'uploader' => 'basic',
'return' => add_query_arg( 'updated', 'true', get_site_url() . '/application/#application-form'),// return url
));
?>
FUNCTIONS.PHP:
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
function my_pre_save_post( $post_id ) {
// bail early if not a new post
if( $post_id !== 'new_post' ) {
return $post_id;
}
// vars
$title = $_POST['acf']['field_58fd9bf56904e'];
// Create a new post
$post = array(
'post_status' => 'publish',
'post_type' => 'ansokningar',
'post_title' => $title
);
// insert the post
$post_id = wp_insert_post( $post );
// now this is where I retrieved the submitted image id
// from the form and attached it to the post
attachment_change_parent( $_POST['acf']['field_59778ef3151e5'][0], $post_id );
// return the new ID
return $post_id;
}
function attachment_change_parent($aid,$pid) {
$update_attachment_post = array(
'ID' => $aid,
'post_parent' => $pid
);
wp_update_post($update_attachment_post);
}
add_action('acf/save_post', 'my_save_post');
function my_save_post( $post_id ) {
// bail early if editing in admin
if( is_admin() ) {
return;
}
// vars
$post = get_post( $post_id );
// email data
$to = array($email, '');
$headers = 'From: XX <[email protected]>' . "\r\n";
$subject = 'Application Confirmation';
$body = 'Thank you for your application!';
// send email
wp_mail($to, $subject, $body, $headers );
}
Your problem is here
'post_id' => 'new_post',
in ACF 5 there is a built in pre_save_post filter that runs before yours and the value of ‘new_post’ triggers this filter which creates the new “Post”.
Use something else like
'post_id' => "new_{$custom_post_type}",
Hi, John. I think this might be the root of a problem that I’m experiencing with my pre_save_post filter. If I comment out the
if( $post_id != 'new_post' ) {
return $post_id;
}
bit then it works. Your comment above suggests that I should change it to new new_{$custom_post_type}
, my post type is ‘role’ but $post_id != 'new_role'
doesn’t do the trick. What am I missing?
NB the documentation says $post_id != 'new'
but that doesn’t work either.
Thanks
To use your own pre_save_post filter the value of the “post_id” argument when calling acf_form() must match the id use in the check within your pre_save_post filter.
If you use the post_id of “new_form” when calling acf_form() then the built in pre_save_post filter will be used and the post ID will be set to a real post ID and the post will already be created before your filter is called. The type of post that is created will be based on the value in the “new_post” argument you use when you call acf_form(), which defaults to “post”.
If you use the built in method of creating a new post, then you don’t really need your own pre_save_post filter, because the reason to use it is to do things before the post is saved, for example getting the title from a custom acf field and using that to create the new post.
Thanks for the quick reply, John. Actually, I solved my problem. The $post_id
var was ‘new_post’ but my filter was being fired after the post had been created and was thus returning the created post ID. I changed the priority to 1 and everything fell into place.
I’m using the pre_save_post
filter because I need to get the values from my fields in order to structure the new post (including meta and tax) and I don’t believe that I could do that with the new_post
argument – unless I’ve missed something.
The topic ‘acf_form() not working in ACF 5’ is closed to new replies.
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.