Tagged: current, Current Post, custom post type, front-end-form, id, LayersWP, post, post_id
- This topic has 6 replies, 2 voices, and was last updated 5 years, 7 months ago by
Steve.
-
AuthorPosts
-
-
July 1, 2016 at 4:32 pm #6870
rharriganMemberFirst time posting to the Piklist Community, but I am very willing to get involved. I will have other tickets, but I’ll start with this one. Thanks for your work.
I want to save info from the Front End Form (FEF) on the current post. I stumbled on a comment somewhere that said there is no direct functionality because multiple posts can be updated in one FEF submisison, so I am looking for a way to overload that. Currently, my code only creates a new post of the same below post type.
The following topics seemed most relevant:
– https://piklist.com/learn/doc/arent-fields-saving/
– https://piklist.com/support/topic/how-can-i-get-the-id-of-new-post-created-by-front-end-form/When using ‘ID’ as in
$id = get_the_ID(); // Where to save this form piklist('field', array( 'type' => 'hidden' ,'scope' => 'post' ,'field' => 'post_type' ,'value' => 'custom_post_type_1' )); // New post ID piklist('field', array( 'type' => 'hidden' ,'scope' => 'post' ,'field' => 'ID' ,'value' => ($id ? $id : '') ,'required' => false, ));I get a “Class ‘Layers_Widget_Migrator’ not found” conflict with my LayersWP theme. Don’t know what’s going on there yet. I do not get a conflict when the field using
$id = get_the_ID(); // Where to save this form piklist('field', array( 'type' => 'hidden' ,'scope' => 'post' ,'field' => 'post_type' ,'value' => 'custom_post_type_1' )); // New post ID piklist('field', array( 'type' => 'hidden' ,'scope' => 'post' ,'field' => 'post_id_backend' ,'value' => ($id ? $id : '') ,'required' => false, ));$id successfully fills. I am using version 0.9.9.9. I tried using code from @mcmaster, but I’m not too sure.
add_action( 'piklist_save_field-post', 'my_grab_new_post_id' ); function my_grab_new_post_id ( $fields ) { global $my_new_post_id; $my_new_post_id = $fields['post_id_backend']['object_id']; } add_filter( 'wp_redirect', 'my_stick_new_post_id_in_redirect', 10, 2 ); function my_stick_new_post_id_in_redirect ( $location, $status ) { global $my_new_post_id; return( $location."?ID=$my_new_post_id" ); } -
July 5, 2016 at 10:55 am #6884
SteveKeymaster@rharrigan– Welcome to the Piklist community!
In the built-in Piklist Demos there is a new Post form you can use as a starting point. Check out:
wp-content/plugins/piklist/add-ons/piklist-demos/parts/forms -
July 5, 2016 at 4:32 pm #6892
rharriganMemberThanks @Steve, but I didn’t see anything in that file regarding saving to an already created CPT. I fixed the dependency issue (LayersWP and Piklist weren’t playing nice in this special circumstance), so that’s no problem anymore.
Now, I have another problem. Using the code I provided earlier:
$id = get_the_ID(); // Where to save this form piklist('field', array( 'type' => 'hidden' ,'scope' => 'post' ,'field' => 'post_type' ,'value' => 'custom_post_type_1' )); // New post ID piklist('field', array( 'type' => 'hidden' ,'scope' => 'post' ,'field' => 'ID' ,'value' => ($id ? $id : '') ,'required' => false, ));Piklist not only creates a new post (which I don’t want), but it creates ANOTHER new post with the meta from my specified post (from $id). When I submit, I now create two posts.
If it helps, I found this in class-piklist-form.php on line 3392:
$id = isset($object['ID']) ? wp_update_post($object) : wp_insert_post($object);How do I save the post meta on the front-end of the post I want to update?
Thanks,
Ryan
-
July 5, 2016 at 9:53 pm #6893
SteveKeymasterYou can use a url like this to edit a post that is already created:
http://mydomain.com/my_form/?_post[ID]=84 -
July 5, 2016 at 11:32 pm #6894
rharriganMemberI’m sorry, I don’t understand what you are saying. I am now using
add_action( 'piklist_save_field-post', 'my_grab_new_post_id' ); function my_grab_new_post_id ( $fields ) { global $my_new_post_id; $my_new_post_id = $fields['ID']['object_id']; } add_filter( 'wp_redirect', 'my_stick_new_post_id_in_redirect', 10, 2 ); function my_stick_new_post_id_in_redirect ( $location, $status ) { global $my_new_post_id; return( $location."?_post[ID]=$my_new_post_id" ); }in my functions.php but I the redirect is not doing anything and I am still creating two posts. What code should I use to implement the example URL you supplied?
Best,
Ryan
-
July 6, 2016 at 4:56 pm #6907
rharriganMemberI found the issue. The two fields with scope ‘post’ where creating to objects – one with an ID and one without (class-piklist-form.php 2942-2968). The object with an ID saved as a revision post (accounting for one of the two posts). The object without an ID went to wp_insert_post. The foreach on 2991 is responsible. I got around this by deleting one of the ‘post’ fields and adding ‘object_id’ => $id
$id = get_the_ID(); // Where to save this form piklist('field', array( 'type' => 'hidden' ,'scope' => 'post' ,'object_id' => ($id ? $id : null) ,'field' => 'post_type' ,'value' => 'custom_post_type_1' )); /* // New post ID piklist('field', array( 'type' => 'hidden' ,'scope' => 'post' ,'object_id' => ($id ? $id : null) ,'field' => 'ID' ,'value' => ($id ? $id : '') )); */It seems to be working on my end. Please let me know if I’m doing anything wrong. If not, I’ll address the other related posts:
– https://piklist.com/support/topic/front-end-form-update-a-cpt/
– https://piklist.com/support/topic/frontend-forms-in-template-files/ -
July 11, 2016 at 11:03 am #6932
SteveKeymasterLooks good!
-
-
AuthorPosts
- You must be logged in to reply to this topic.