I want to create a form that creates a yoga studio post type I want to be able to add yoga teachers (A second custom post type) to the yoga studio while the user is creating their yoga studio post. Is this possible?
Hi, it's possible but would probably require custom code to implement effectively. For example, you could add generic text input field(s) to the new Studio Form so you can capture new Teacher name(s). You could also add a generic multiselect or checkboxes group field with options defined by a View of Teachers, so your Users could select from existing Teachers. However, it would require you to use custom code on the backend to validate those text entries (so no one tries to add the same Teacher twice, for example) and then use custom code with the cred_save_data API to generate the corresponding new Teacher posts using wp_insert_post. Finally to associate the selected and new Teachers with the new Studio post, you would use the post relationships API toolset_connect_posts.
I can offer some assistance with any of those APIs if you'd like, but I wanted to see if this sounds like something you're interested in investigating further.
Hi Christian,
Thanks for the advice.
I dont actually need to create teachers using the form, only to access existing teacher custom posts that already exist and associate them with the studio custom post that will be created with the form (in a many to many relationship)
I will also want to do it the other way round so that teachers can select from the studios that have been created when creating their teacher post.
Regards,
David
Okay yes it's a bit simpler if the relatable posts already exist. Is there a specific part of this process you need assistance with? I think these are the major components:
- Create a generic multiselect or checkboxes field with options defined by a View. Here's a checkboxes example:
Form code
[cred_generic_field field='select_teachers' type='checkboxes' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="select-teachers-in-studio-form"] ]
}
[/cred_generic_field]
View of Teachers code
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop>
{"value":"[wpv-post-id]","label":"[wpv-post-title]"},
</wpv-loop>{"value":"all","label":"all"}
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found][/wpv-no-items-found]
[wpv-layout-end]
Be sure to check the checkbox to disable the wrapping div for this View, since you only want the raw data to pass into the generic field.
- Use the cred_save_data API to collect those selected options from the $_POST superglobal:
$selectedTeachers = $_POST['select_teachers']; // array of teacher IDs
- Use the toolset_connect_posts API to programmatically associate each of those collected Teachers with the new post being created. Example of looping over an array to associate multiple posts:
https://toolset.com/forums/topic/post-relationships-not-creating-where-there-are-multiple-forms-on-a-page/
https://toolset.com/forums/topic/continued-connect-multiple-child-posts-to-parent-in-one-move/
- Apply those same concepts to the Form that approaches from the opposite direction.
API definitions:
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
Woops, I forgot to mention something important about generic multiselect fields. There is currently a known issue that generates a PHP warning message. It will be fixed in the next release of Forms. In the meantime, a patch file is available here: https://toolset.com/errata/php-error-when-using-generic-multiselect-form-fields/