Home › Forums › Front-end Issues › Reassign random Post ID with actual Post ID for new post
I am currently using the following code for creating new post:`acf_form(
array(
'post_id' => 111222,
'form' => false,
'field_groups' => array( 30 )
)
);`
The number 111222 is randomly generated, because it doesn’t have Post ID yet. And I use the following code to save the file field:`function my_save_post($post_ID) {
if ( isset($_POST['field_689b0ed7249ab']) ) {
update_field('field_689b0ed7249ab', $_POST['field_689b0ed7249ab'], $post_ID);
}
}
add_action('save_post', 'my_save_post');`
This code works fine when I edit a post, but not for the new post. It looks like the Media Manager will stick to post ID number 111222, because the file that was uploaded before using this number can be seen later, but it’s not reflected with update_field(). Is there a way to change update 111222 to the correct post id? Something like`function my_save_post($post_ID) {
if ( isset($_POST['new_post_id']) ) {
change_post_id('field_689b0ed7249ab', $_POST['field_689b0ed7249ab'], $post_ID);
}
else {
update_field('field_689b0ed7249ab', $_POST['field_689b0ed7249ab'], $post_ID);
}
}
add_action('save_post', 'my_save_post');`
—————————————–
EDIT: After some digging, I see that uploaded file is saved as attachment in “post” table and with the file name in “postmeta” table, which is good. I see also that in the “post” table, the field “post_parent” contains the 111222 value for the new post. So I guess I just need to change that field to with the new Post ID.
I see when editing a post, uploading a file, it creates 3 rows in the post_meta which are
META_KEY META_VALUE
_uploaded_file field_689b0ed7249ab
uploaded_file 345
_edit_lock 1383884182:1
So I guess I have to somehow create this postmeta rows, correct? Is there a way to easily create these rows after claiming the orphan 111222 attachment file?
Or do you have other more elegant solution?
Thank you.
Hi @acfbigfan
Lots of quesitons above, and I’m a bit confused about what exactly you need help with, but lets first start with the form.
There is a code example here which shows how to use the pre_save_post filter to modify the post_id of a new post:
http://www.advancedcustomfields.com/resources/tutorials/using-acf_form-to-create-a-new-post/
I have a feeling, your issue is not actually with the post, but with the uploaded images, right?
I would simply get the image data via get_field, then use the image ID to update the actual attachment row in the database.
WP has a function called update_post. And if you send through an array containing the ID and also the post_parent value you want, you can modify this media attachment.
If this does not answer you question, could you perhaps simplify you question?
Thanks
E
Thank you for getting back to me. Simply put, I already have a custom form and I am calling the file upload field by calling
acf_form(
array(
'post_id' => 'new',
'form' => false,
'field_groups' => array( 30 )
)
);
As you can see, I am not calling the whole form (<form> tag and submit button), only the fields
'form' => false,
So I can’t use ‘acf/save_post’ because it won’t be triggered. It’s because my custom form uses AJAX to save the data calling POST via jQuery. So I use the following action to save ACF fields (I’ve simplified the field name)
my_save_post($post_ID) {
update_field('field_text', $_POST['field_text'], $post_ID);
update_field('field_file', $_POST['field_file'], $post_ID);
}
add_action('save_post', 'my_save_post');
In this case, the field_text is saved, but not field_file because when the file information is inserted as attachment, it doesn’t save the “parent_post” ID correctly and becomes an orphan. And these 3 postmeta rows aren’t created either
META_KEY META_VALUE
_uploaded_file field_689b0ed7249ab
uploaded_file 345
_edit_lock 1383884182:1
which are created normally if I upload a file when editing a post from the frontend if post_id is assigned.
Thank you!
Okay, I see that _edit_lock value is “timestamp:1”, so managed to add required metaposts. Instead of using
'post_id' => 'new'
I used time() to assign the timestamp value
'post_id' => $timestamp
then replaced the attachment post_parent containing that timestamp value with the new Post ID later and it worked like a charm.
Thank you once again for your great plugin and support!
The topic ‘Reassign random Post ID with actual Post ID for new post’ 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.