I have a CPT called Doctors and another one called patients. Both these CPT are in a many-to-many relationship. I also have a view which a doctor can use to search for patients. What I am trying to do is to let this doctor to be able to connect to those already-existing patients as children by pressing the add patient link/button (see screenshot of a test page, or see the live example hidden link). There is an intermediary form but I don't want the doctor to fill it at this point just yet. I just need doctors to be able to connect patients to their CPT at a button press without needing to go to another page with a relationship form and again select the patient.
Now in one-to-many relationships, I can do this by actually creating a CRED form that automatically selected the current user (doctor) as the parent and then I can hide this parent field and then rename the submit button to "Add Patient" and let only the submit button of the CRED form appear. I can then add that CRED button to the view mentioned above so that it appears in the same row as every patient, and that way I can let the doctor add any of the patients CPT in the list as children without having to leave the page or fill any form.
Now in many to many relationships that same approach doesn't seem to work with relationship forms as I can't simply add a parent selection field and then let it select the current user automatically. I need the relationship form to inherit the patient from the view and get the parent (doctor) from the current user so that I can hide those fields and only show the submit button (add patient button).
What is the best approach to achieve this in many-to-many relationships?
Hello. Thank you for contacting the Toolset support.
Well - I checked with the relaitonship forms shortcode that displays the parent and child select2 field that will allow you to connect the posts but unfortunately there is no such feature available to preselect the parent and child posts with respective select2 fields.
If you agree - I can take this as a feature request.
Yes please. I think that this is a very important feature request and I think it should be there by default just like post forms.
Do you have any workarounds for me? I just want the user(doctor) to search for patients by their phone number, then he gets the name of the patient (just to be sure it is really the patient he wants) and then he can add him/her (connect to it). Can you suggest anyway for me to do this or something similar for now?
Any suggestion of a different way to do it until the feature is added? As I said, I just need to search for patients by phone numbers and then be able to connect to any of the resulting patients.
For example, setup a link to pass the "Doctors" ID and "patients" ID to a wordpress page, in that page use above function to setup the post relationship
I was not aware that this is possible through PHP. I am not a PHP developer but I will try to work my way around. I think I need to put this php code in functions.php. right?
You gave me hope that this problem may be solved. Thank you.
Kindly leave the thread open as I may need a little nudge in the right direction. I'll try this solution tonight and give you feedback.
I am sorry for the delay. I was trying to fix my problem based on your suggestion and this is what I came up with (please bear in mind that I don't have any background in PHP so pardon my funny code):
I thought of adding a shortcode in php that connects both doctors and patients through the api you provided (I called it [wpv-connect-docpatient]). I will place this shortcode in my view that displays the patient list (in screenshot above) so that when the user presses 'Add patient' this runs the shortcode (Q1 is that possible, can a button run shortcode?)
My main problem now is in the php code .. it should be something like this:
//creates a shortcode to connect doctor (as parent) to patient
add_shortcode('wpv-connect-docpatient', 'connectdocpt_shortcode');
Function connectdocpt_shortcode($post_id) {
toolset_connect_posts( 'doctor-patient', $parent_id, $post_id);
}
The problem is: I know how to get doctor and patient ids in toolset but don't know hot to get them in PHP:
Q2 In toolset the patient id is displayed as the current post id by the view loop I am using in screenshot. In PHP I guess I get it by $post_id?
Q3 In toolset I get doctor id by a view [wpv-view name="get-doctor-from-current-user"] that returns the raw clean id of doctor correctly (current doctor is the post type with current user as author). How to get it in PHP?
Since it is a custom PHP codes problem, please provide a test site with the same problem, fill below "private detail box" with login details and FTP access, also point out the problem page URL, how and where do you want to add the button, where I can edit your custom PHP codes, I need a live website to test and debug your codes, thanks
Well - as I updated you that we have take this as a new feature request. There is no such feature to connect it directly until you have some custom programming using AJAX and that is beyond the scope of our support policy.
If you need custom programming for your project, please feel free to contact our certified partners:
=> https://toolset.com/contractors/
I really do not understand your response!! I am not asking for a strange feature out of the scope of toolset like a booking system or calculations etc as some customers do (and they got help even for these out-of-the-scope things by the way)... I am only asking for a very simple thing at the core of toolset: to connect two CPT in a many to many relationship through automatically adding the parent and child. It is something that one-to-many relationships can already do for ages and it should naturally be available in many to many as well because it just doesn't make sense not to be able to do it.
Now that I also know that you have a toolset command that can help me(toolset_connect_posts), and you say it is out of scope?! It is a TOOLSET command. How can that be out of scope?! Denying me help with a toolset command as a workaround for a toolset problem is really upsetting and unacceptable !!!
I want an example code to work with to know how to use this command to connect two posts!! This is the least you can do and I don't think that this is too much to ask!
I've gone through this ticket replies and I see that you are right. this should be implemented.
I've already tried to implement a workaround specifically for you but unfortunately it's not possible.
The problem is that there is no hook available that works after submitting the relationship form.
We neither have an API for the relationships forms. The good news is that we have an open ticket in our internal queues for that to be implemented.
The reason why we will implement this is that we already have the CRED API for post forms: https://toolset.com/documentation/programmer-reference/cred-api/ and we need a similar one for relationship forms to make our clients able to control the relationships forms in the way they wish.
I can't promise you when this can be implemented but I will try to push forward implementing this as soon as possible.
Thank you very much for your response and for trying to come up with a workaround for me. I really appreciate it.
I would add that we also need to be able to use a shortcode to pass an id as the parent/child field value in a relationship form.
For example, in a post form we cab do this:
[cred_field field='@provider-service-order.parent' select_text='--- not set ---' value='[wpv-post-id]' class='form-control' output='bootstrap']
If this is available for relationship forms, it would solve my problem as I would be able to automatically select parent/child without the need of any hooks or PHP. This was actually my original approach to solve this problem but it didn't work in relationship forms.
I really hope that you add that as a feature request as well, to make relationship forms as versatile as post forms.
Again, thank you very much for your response and care.