Startseite › Toolset Professional Support › [Gelöst] Post and related custom post type on same front end form
Problem:
Post and related custom post type on same front end form
Solution:
You should use the Toolset form's hook "cred_save_data" and then use the wp_insert_post() function to create a new entry automatically and then use the post relationship API function toolset_connect_posts().
You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/post-and-related-custom-post-type-on-same-front-end-form/page/2/#post-1354943
Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Kolkata (GMT+05:30)
Tags: Toolset Forms
Dieses Thema enthält 19 Antworten, hat 2 Stimmen.
Zuletzt aktualisiert von Daniel Stadeli vor 5 Jahren, 3 Monaten.
Assistiert von: Minesh.
Tell us what you are trying to do? Assigning bible references (passages or verses) to posts
every post can have zero to many relationships to a custom post type called 'Biblebooks'
The relationship between posts and biblebooks is many to many and has fields called 'chapter', 'vers', 'verse-to'. This allows me to model
Matthew -> 1,1-5 <- Post 1 , meaning the bible reference in Matthew chapter 1, verse 1-5 is assigned to this post
I was able to design this relationship, sample screenshot attached.
I want to enter the post fields on the front end in a form (I already have that) and in the same form I want to enter bible reference, eg. creating multiple relationships to Biblebooks and assigning chapter and vers(es). Every time I save a bible reference it needs to be listed on the screen and then in the end everything is saved with the post in the form.
Every bible reference assigned to a post is a NEW relationship.
eg. ONE form for both Post and related post type:
----------------------------------
| Post Title
|Description
|Image
|custom fields A, B, C
|- Bible reference 1: Book (drop down on post title biblebooks), type chapter, verse, and optional verse to
|- Bible reference 2: ...
|- Bible reference 3: ...
| +add bible reference
|SAVE (button)
---------------------------------
Is there any documentation that you are following? nothing on the subject can be found. It only talks about making a separate form for the related custom post type, but I want it all in the same form
Is there a similar example that we can see? versteckter Link at the very bottom, but is not set up correctly. I'm attaching the sample form inside the New Post form on the backend as an example
What is the link to your site? versteckter Link
Can you give me directions on how to go about it? Like a recipe to follow, focusing on the front end form. Then I can implement and come back with questions. Thanks
Hello. Thank you for contacting the Toolset support.
Normally, Toolset Form support one form for the post type for which the form is made.
If you wanted to combine all post types fields within single form it's possible but it will require to write some custom code using the Toolset form's hook "cred_save_data" and need to use post-relationship API function toolset_connect_post() to connect the related posts.
More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
You need to add additional fields which belongs to your parent or related post type to your form using the Generic fields
=> https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
But, I see you wanted to add multiple bible reference instances, so that is not possible, or you need to add as many custom fields. For instance, if you wanted to add 5 bible entries, you need to add each field 5 times.
So, its always better to add a add new bible link that links to the form that will add a new bible single entry at a time.
pls. give me a couple more days, I've asked a third party developer to help with this. I'll close the ticket once it's working. thanks Minesh.
Ok - sure.
Hi,
On a custom post type Bibelverse I have custom fields: Book, Chapter, Verse from, Verse to
I don't want a Post title field, but even if I disable it, it will still show up.
So since it's needed, I want to at least fill it in automatically (formula see screenshot).
What is the procedure to do that and where do I put such code?
Also, I need to be able to enter bible references on the front end, while creating a new post. (see attachment for posts and cpt relationship). I was unsuccessful, since the post form disappears as soon as I hit save, or the bible reference dissappears as soon as I hit save. But I need to save 1 post and multiple references together, same as in backend, but on frontend. Very basic Database application task, seems to be hard here... Can you pls. help?
You can see the behaviour here: versteckter Link
you still have access to the db.
I checked the following screenshot:
=> https://toolset.com/wp-content/uploads/2019/09/1349707-selk_custom_field_mandatory_post_title.jpg
With the above screenshot I see you want to combine the post title using custom field values - Do you mean that you want to generate such dynamic post title for your post type Bibelverse using Toolset form - correct? If yes:
- You can use the Toolset Form's API hook - cred_save_data:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
For example:
//Create a dynamic post title by the CRED form. add_action('cred_save_data','func_custom_post_title',10,2); function func_custom_post_title($post_id,$form_data) { if ($form_data['id']==9999) { $name = get_post_meta($post_id, 'wpcf-name', true); $email = get_post_meta($post_id, 'wpcf-email', true); $title= $name. '-' . $email; $args = array('ID' => $post_id, 'post_title' => $title); wp_update_post($args); } }
Where:
- Replace 9999 with your original form ID and ajdust the fields and code accordingly as required.
To connect the posts - you can use the post-relationship API function: toolset_connect_posts()
More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
Thanks Minesh,
I got the first part to work. Here's the finished code, in case it would help someone else:
//Create a dynamic post title by the CRED form. add_action('cred_save_data','func_custom_post_title',10,2); function func_custom_post_title($post_id,$form_data) { if ($form_data['id']==1067) { $buch = get_post_meta($post_id, 'wpcf-bibelbuch', true); $chapter = get_post_meta($post_id, 'wpcf-chapter2', true); $verse = get_post_meta($post_id, 'wpcf-verse-from', true); $verse_to = get_post_meta($post_id, 'wpcf-verse-to2', true); //one verse or range? $range = $verse; if (!empty($verse_to)) { $range .= '-'.$verse_to; }; //concatenate title $title= $buch. ' ' . $chapter .','. $range; $args = array('ID' => $post_id, 'post_title' => $title); wp_update_post($args); } }
I first put this code under Toolset > Settings > Custom Code and it wouldn't do anything. Then I moved it to functions.php of my child theme and it worked. I don't understand what the Toolset Custom Code section is for... Do you mind explaining?
The CPT Bible-verses is set up with only custom fields and NO post title (see screenshot)
This CPT is linked to Posts
When I go through the backend to edit or add a new post (see screenshot), and want to add the related bible verses, the Post title field comes up (see screenshot).
Is there a way to disable this field and calculate it automatically, the same way as on the front end?
Minesh,
3) the main problem of this ticket remains unsolved: Create parent and related child post on the same form.
I'm following this tutorial: https://toolset.com/documentation/post-relationships/selecting-parent-posts-using-forms-create-child-items/
I'm working on this page: versteckter Link . The top part is the Create new Post form, the bottom part is a create new child post form> bible reference form (one post can have many bible references).
Each form works separately, but they don't work in harmony. Once I enter a new post on top and save it, anything entered in the second form is lost. If I first save the bottom form (bible references), all the information entered for the post form is lost.
You gave me a link to the documentation with one command: "toolset_connect_posts( 'book-author', 5, 7 );". but that really doesn't help me. How do I use it?
What are the steps to solve this riddle? Do I have to save the post form in the background first, before the bible references can be entered? If so how?
I would appreciate a more detailed step by step example with hopefully some code I can adapt. The help you gave me with the dynamic post title was amazing, I need something like this here.
Thanks
I'm working on this page: versteckter Link . The top part is the Create new Post form, the bottom part is a create new child post form> bible reference form (one post can have many bible references).
===>
I do not able to see the form. I think I need wp-admin access details to see your form.
You can not merge two forms together but if you want to add child form fields to your parent form, you should do it and add it using the Generic field:
=> https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
(so, you have parent form and add all child post fields using Generic field to your parent form that means you will have at last combine form with both parent + child post types fields)
Then, on submit, again, you can use the cred_save_data action and use the post-relationship API function toolset_connect_posts() to connect your child with parent.
More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
Minesh, I have added login credentials for this site to another ticket. How can I add it here? I can't find an option for adding login information.
I'm attaching a screenshot of what you should see.
I already saw your 2 links from before. You gave me a link to the documentation with one command: "toolset_connect_posts( 'book-author', 5, 7 );". but that really doesn't help me. How do I use it?
Please share wp-admin access details and once I will review your setup and post-relationship structure, I will get back to you but first of all, I need access details.
I have set the next reply to private which means only you and I have access to it.
Ok - thanks for access details.
I see you added both parent and child form on single page and as I understand you want to combine both the forms.
But please note that - at a time you can create only one entry for both parent and child post types. Is that ok for you?
Where what we will do is - add all the child post type form fields to your parent post type form. We will use the following form and combine both parent and child form so we will have a single form. Please let me know if thats OK and I will procced with next steps.
So, I've created and combined the fields of both parent and child post type fields using the followng form and the form looks like this:
=> versteckter Link
Now, we will use the cred_save_data hook and when we sumit the form we will create the child entry automatically. Sounds good?
Minesh,
Reg. the same issue, I have this code which gives me an "technical dificulties" error on the front end. It is the part that is writing our a value to the custom field 'wpcf-verse-to2', everything else works on it's own.
//Create a dynamic post title by the CRED form. add_action('cred_save_data','func_custom_post_title',10,2); function func_custom_post_title($post_id,$form_data) { if ($form_data['id']==1067) { $buch = get_post_meta($post_id, 'wpcf-bibelbuch', true); $chapter = get_post_meta($post_id, 'wpcf-chapter2', true); $verse = get_post_meta($post_id, 'wpcf-verse-from', true); $verse_to = get_post_meta($post_id, 'wpcf-verse-to2', true); //one verse or range? $range = $verse; if (!empty($verse_to)) { $range .= '-'.$verse_to; } else { $meta_args = array('post_id' => $post_id, 'meta_key' => 'wpcf-verse-to2', 'meta_value' => $verse, ); }; //concatenate title $title= $buch. ' ' . $chapter .','. $range; $args = array('ID' => $post_id, 'post_title' => $title, 'wpcf-verse-to2'=> $verse_to, ); wp_update_post($args); if (empty($verse_to)) { update_post_meta($meta_args); }; } }
in the else statement I prepare the $meta_args and on the last statement I try to write it. Can you see where I made a mistake?
Can you please just let me know that - the following form I've created is OK:
=> versteckter Link
If you can confirm yes/no, I will try to add the required code and share with you.