Problem: I have two custom post types in a many-to-many (M2M) relationship. In a Form that creates new child posts, I would like to allow Users to select multiple parent posts and create those relationships automatically.
Solution: Usually M2M relationships are managed in Relationship Forms after both parent and child posts are already created. To create M2M relationships between posts in a new child post Form, you must use a generic multiselect field and custom code. Here is the general approach:
1. Create a View of one of the parent post types. Click "Loop Wizard" above the Loop Editor and select the "list with separators" display type. In the next screen of the popup, click "Add a field" and add the post title, then click "Finish". In the loop editor, replace the code inside the wpv-loop tags with the following code:
<wpv-loop> [wpv-item index=1] {"value":"[wpv-post-id]","label":"[wpv-post-title]"} [wpv-item index=other] ,{"value":"[wpv-post-id]","label":"[wpv-post-title]"} </wpv-loop>
Be sure the checkbox "Disable the wrapping DIV around the View" is checked, and delete all the content from inside the wpv-no-items-found shortcode, like so:
[wpv-no-items-found][/wpv-no-items-found]
This View will be used to generate JSON-like options for each parent post in one generic multiselect field in Forms. You will repeat this process to create Views of any additional parent post types. Note the slug of this View for step 2 below.
2. Edit the child post Form using Expert Mode in the Form Builder. Click "Add generic fields" above the code editor and choose a multiselect generic field. In the popup, choose the option to "Get options from a shortcode". Insert a View shortcode that includes your parent post type View slug in the name attribute, like so:
[wpv-view name="your-parent-post-type-view-slug"]
Save the field, and the expert builder will insert a new shortcode for you automatically in the Form contents. It will look like this:
[cred_generic_field type='multiselect' field='your-multiselect-field-slug'] { "required":0, "options":[ [wpv-view name="your-parent-post-type-view-slug"] ] } [/cred_generic_field]
3. Test the Form on the front-end of the site. You should see a multiselect field appear at this point with options for each parent post.
4. If the multiselect field is displaying correctly, now you need to add some PHP code using the cred_save_data hook that will capture the selected values from that multiselect field and loop over those values when the Form is submitted successfully. Inside this loop over the selected values, you will call the toolset_connect_posts API to create relationships between each selected option and the new child post. You can add this code in a child theme's functions.php file, or in a new custom code snippet in Toolset > Settings > Custom Code tab. Here is an example of such a cred_save_data hook:
add_action('cred_save_data', 'tssupp_associate_multiple_related_posts',10,2); function tssupp_associate_multiple_related_posts($post_id, $form_data) { $forms = array( 123 ); $relationship_1_slug = "parent-child-relationship-slug"; $ms_field_slug = "your-multiselect-field-slug"; if ( in_array( $form_data['id'], $forms ) ) // test if the form being submitted is in the list of form IDs we want to target { $ms_selections = $_POST[$ms_field_slug]; // the $_POST superglobal contains an array of the selected parent post IDs foreach($ms_selections as $ms_selection) { toolset_connect_posts( $relationship_1_slug, $ms_selection, $post_id); // while looping over selected parents, connect them to the new child post programmatically } } }
In this example, you would replace 123 with the numeric ID of the child post Form, or a comma-separated list of child post Form IDs if there is more than one new child post Form. You would replace parent-child-relationship-slug with the slug of the relationship between the parent post type and the child post type. You would replace your-multiselect-field-slug with the slug of the generic multiselect field you created in the Form builder. Inside the "if" condition, you can create new foreach loops for each parent post type, but let's hold off on that for now and just get one parent type saving correctly.
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.
Our next available supporter will start replying to tickets in about 2.23 hours from now. Thank you for your understanding.
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 7 replies, has 2 voices.
Last updated by 3 years, 11 months ago.
Assisted by: Christian Cox.