Skip Navigation

[Resolved] Toolset forms many to many

This support ticket is created 5 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by Christian Cox 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1189639

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?

#1189792

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.

#1189828

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

#1189896

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

#1189980

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/