Home › Forums › Front-end Issues › Frontend Edit & Create Posts
Hello,
First of all let me say that this plugin has been a huge time saver and i really like it so much that i purchased all the addons.
I need to create a frontend forms that creates new posts, the tutorial works perfectly, what i also need to do is to update a custom user meta field, for this scope i was going to add my custom code after this line contained in the tutorial here http://www.advancedcustomfields.com/resources/tutorials/using-acf_form-to-create-a-new-post/
// insert the post
$post_id = wp_insert_post( $post );
the code after that would update a usermeta.
Now my concern is: if i create a custom page where i pass the id of a post to edit/update it (not create it) will the update user meta function added through
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
trigger when i update the post? Because i don’t want that to happen. Is there any way i could prevent that from happening ? Any suggestions ?
Thank you so much 🙂
Hi @alexgold05
Yes. Each time your ACF form submits, the acf/pre_save_post
filter will be run.
Perhaps you need to add some logic to your usermeta code which only saves if a new post has been inserted.
Feel free to post your entire code if you need any help.
Thanks
E
Hi,
Thanks for your reply, i managed to make it work however i have an issue with a few fields.
The create new post form, successfully creates the listing and i’m also grabbing the post title and the post content and the featured image through custom fields. This works.
I have then attempted to modify the action so instead of inserting a new post, he would update it. The edit works but some of the fields are not updating. For example when i modify the post and i attempt to modify the custom field that has been used for the post title, it isn’t updating the post title but just the custom field.
This is part of the code used.
global $current_user;
// check if this is to be a new post
if( $post_id != 'new_listing' )
{
return $post_id;
}
// Create a new post
$post = array(
'post_status' => get_field('default_listing_submission_status','option') ,
'post_title' => $_POST["fields"]['field_522f30035c85b'],
'post_content' => $_POST["fields"]['field_5230848453094'],
'post_type' => 'listing' ,
);
$post_edit = array(
'post_status' => get_field('default_listing_submission_status','option') ,
'post_title' => 'test',
'post_content' => 'test2',
'post_type' => 'listing' ,
);
//add listing
if( $post_id = 'new_listing' ) {
$post_id = wp_insert_post( $post );
} else {
$post_id = wp_update_post( $post_edit );
}
Do you have any idea why the form wouldn’t update the post title or the content ?
Thank you. If needed i can post the whole code but i don’t anything else in the action is interfering with it.
Hi @alexgold05
I can see at the top of your code that if the $post_id != ‘new_listing’, you return the $post_id.
This return
will stop any further code from working!
Perhaps you need to change your code to look more like this:
<?php
global $current_user;
// check if this is to be a new post
if( $post_id == 'new_listing' )
{
// Create a new post
$post = array(
'post_status' => get_field('default_listing_submission_status','option') ,
'post_title' => $_POST["fields"]['field_522f30035c85b'],
'post_content' => $_POST["fields"]['field_5230848453094'],
'post_type' => 'listing' ,
);
$post_id = wp_insert_post( $post );
}
return $post_id;
?>
I have taken the liberty of removing the update_post functionality as I don’t see how it does anything useful for the above code.
Thanks
E
Dadmore,
This plugin (acf-frontend-display) is interesting, but it should include a couple disclaimers:
It doesn’t work with the PRO version of acf.
The author so far has stated they are not (yet?) intending to support the PRO
version.
It does not use the native acf_form code. The author modified the code for this plugin.
Again, interesting plugin idea, just feel it should include these details up front.
The topic ‘Frontend Edit & Create Posts’ 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.