Please acknowledge that this features will be added to Toolset in Future and that it is subject to Custom Code which we support only limitedly.
I am giving here a simplified example, to use if you cannot wait for the features, which you need to adapt to your specific install.
Once the features are added to Toolset, these codes probably would need to be removed.
We assume no responsibility for problems arising with this code, although, there should not be any, I tested it carefully.
But, please acknowledge that this is a custom solution, and not an official solution (which will be included with new features eventually).
Enough of the warnings and disclaimers, here is how you can bulk-connect many posts of one kind, to one post of a kind, in the Front End.
This Example assumes a one-to-many relationship.
However, it should as well be adaptable to a many to many relations.
The main point, however, is, that in the Front End you will always connect ONE to MANY posts, never MANY to MANY as that is simply not possible to manage (which post is connected to which?)
Note that this could as well be done in a View Loop which displays several Toolset Relationship Forms, which with each you can create a new relation.
But, in this case, you will click as many times you want to connect.
That is not what we wanted, we want one form to submit, and with that, you connect 200 posts at once to one post of another type.
Steps:
- Create 2 Post Types
- Create a Relationship, one to many. Note, in my example, one is left, many is right.
- Create 1 Post in the single part of the relationship (left side)
- Create many Posts in the many part of the relation (right side)
- Create a View, and query the "many" part post type (right side)
- Create a valid JSON ouput of Post ID and Post Name in the View loop:
[wpv-items-found]<!-- wpv-loop-start --><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><!-- wpv-loop-end -->[/wpv-items-found]
- refer to this post how to output that JSON as a raw value:
https://toolset.com/forums/topic/cruft-output-by-views/#post-610342
(you will add that function to your functions.php and adapt it)
- Create a Post Form, which edits the "single" part of the relationshiop (it edits post type of "left side")
- In that form add a generic Checkboxes Field and pass a Shortcode as the value (the shortcode is the view you just created)
==> Make sure to use single quotes for the view's shortcode!
- Check that your Form, if inserted to a "left side" post, is displaying the "right side" posts fine (as checkboxes to be chosen)
- Now, create a cred_save_data() hook that takes the Post ID edited, and the Post ID passed in the generic Field, then runs an update to the database using our API to connect the posts:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==10)
{
if (isset($_POST['parents_to_connect']))//this is our generic checkboxes field
{
$post_ids_to_connect = $_POST['parents_to_connect'];
$relationship = 'left-side-post-right-side-post';//our relationship slug
foreach ($post_ids_to_connect as $post_id_to_connect) {
//For each post we want to connect, connect it to the currently edited post
toolset_connect_posts( $relationship, $post_id, $post_id_to_connect );
}
}
}
}
Submit the form, and see that each Post you chose, is no bulk-connected to the one post we edited.
This is a simple approach to allow bulkl editing in the front end.