Home › Forums › Front-end Issues › Unable to detect new_post on acf_form
I have a pretty standard form.
$new_startup = array(
'post_id' => 'new_post', // Create a new post
'field_groups' => array(13526, 13513), // Create post field group ID(s)
'post_title' => true,
'return' => '%post_url%&new-startup=true',
'new_post' => array(
'post_type' => 'startups',
'post_status' => 'draft',
),
'submit_value' => 'Save & Continue',
'html_after_fields' => '',
);
acf_form( $new_startup );
Everything works properly to create the new startup. I also have another form that edits the startup, and both function properly.
However, i’m unable to detect if the post being created is a new post using acf_pre_save_post. If I use:
if($post_id != 'new_post')
or even
if($post_id != 'new')
it does not return true. if I write the $post_id to my log, it outputs the post ID of the newly created post. How could this be if we are in pre_save_post function?
Any help would be great appreciated. How can I properly detect if the form being saved is a new post or an existing post. What am I missing?
just checking, are you adding a filter for the hook acf_pre_save_post
or acf/pre_save_post
, the 2nd one is the correct one.
If that’s right can you post your filter function?
Thanks for the reply!
Here is the filter function.
function sl_acf_pre_new_startup( $post_id ) {
if( $post_id != 'new' ) :
write_log('not new');
return $post_id;
else:
write_log($post_id);
return $post_id;
endif;
}
add_filter( 'acf/pre_save_post' , 'sl_acf_pre_new_startup', 10, 2 );
Using the above, it only ever returns “not new” even though it’s creating (successfully) a new post.
I think I spotted your problems.
In your form setting for acf_form() your setting the id to ‘new_post’
'post_id' => 'new_post', // Create a new post
but in the pre save post filter you’re checking for ‘new’
if( $post_id != 'new' ) :
You need to change these to the same value.
From your first post, the argument in $new_startup
is correct.
Have you tried the following the check on what the $post_id is?
function sl_acf_pre_new_startup( $post_id ) {
write_log('POST ID: '.$post_id);
}
add_filter( 'acf/pre_save_post' , 'sl_acf_pre_new_startup', 10, 2 );
Thanks for the reply. Using that exact function above, I get this:
POST ID: 30673
For now, if you set the priority of your filter to -1 it will work.
This is odd, because I know this was working recently.
John. A priority of -1 on the filter worked perfectly. Thanks for the help man! Cheers and have a great weekend. I’ll keep an eye on the changelog for a fix for this too.
OK, I think I have this, and it’s not a bug.
Fn your form arguments include
'post_id' => 'new_post',
Then ACF creates the new post automatically using the content of
'new_post' => array(
'post_type' => 'startups',
'post_status' => 'draft',
),
If you want to create a new post of the type startups
and you want to handle the details of this yourself in an acf/pre_save_post filter then in your form arguments use
'post_id' => 'new_startups',
and in the pre save post filter use this the test
if( $post_id != 'new'_startups )
I hope this explains things. It seems I was under the same impression you were from reading the documentation.
The topic ‘Unable to detect new_post on acf_form’ 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.