[Resolved] Using the toolset_connect_posts function
This support ticket is created 3 years, 7 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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
add_action('cred_save_data', 'connect_camera_issue_func',10,2);
function connect_camera_issue_func($post_id, $form_data)
{
// if a specific form
if ( $form_data['id']== 7451 && isset($_POST['camera_title']))
{
$camera_title = $_POST['camera-title'];
$tmna_location = get_post_meta($post_id, 'wpcf-tmna-function-or-area', true);
if($camera_title){
toolset_connect_posts(
$relationship = 'tmna-location',
$camera_title,
$tmna_location
);
}
}
}
to link new 'camera issue' post form entries to their respective locations (a different CPT) based upon the camera-title & tmna-function-or-area fields. But it's not working. I checked to make sure that the fields are added to the many/many relationship. Am I missing a step?
if ($form_data['id']==7451) {
if(!empty($_POST['wpcf-tmna-location'])) {
toolset_connect_posts( 'tmna-location-camera-issue', $_POST['wpcf-tmna-location'], $post_id );
I think you had mentioned that I needed to reference the post ID instead of the slug however the CPT doesn't have an ID. The relationship is correct so my question is do I need to reference an different field altogether to make this work?
As I don't have the full scope of the question, I'll have to make the following assumptions:
1. Your website has a relationship with slug "tmna-location-camera-issue" where the "tmna-location" CPT is the parent and the "camera-issue" is the child.
2. This form in question ( ID: 7451 ) is a form to add a new "camera-issue" post.
3. The "wpcf-tmna-location" field is a select type custom field, which programmatically gets options from the "tmna-location" CPT.
If all my above assumptions are correct, then yes the part/extract of the code snippet that you've shared is correct.
In case this doesn't work, please check if any error or warnings are shown on the screen or in the server's error log and also share the link to a page where this form can be seen.
I think you are mostly correct in your assumptions. I don't know that TMNA Location is a parent here. It's simply another provider of data. There are several many-many relationships going on. I suppose holistically everything goes back ultimately to 'tmna location' since it has to terminate back to a location. So if I had to build a hierarchy of parent child relationships for this post form: TMNA Location>Camera>Camera Issue.
Anyway, it might be possible that based upon this hierarchy I'm not implementing the relationship correctly in the function? I'm not aware of any errors. This form is at /add-camera-issue/ and the results display at /cameras/ however the real test would be to look at a location and see if the newly entered issue appears. For example if you entered an issue for C216 it show up in /tmna-location/plano-sequoia-c1/ where there is already one issue I added manually testing the relationship section (at the bottom). The location issues view is /wp-admin/admin.php?page=views-editor&view_id=13412
Please follow the recommendations from that reply and let me know once the website is accessible again and I'll follow-up on this question accordingly.
I've checked the form at "/add-camera-issue/" page by creating a test post and noticed that the first part of the code for the "Camera Title" field and the "Cameras Camera Issues" relationship is working, but the second part for the "Location (optional)" field and the "Camera Issues TMNA Locations" relationship is not.
This is how your existing function's code looks like:
add_action('cred_save_data','func_connect_child_camera_posts',15,2);
function func_connect_child_camera_posts($post_id,$form_data) {
if ($form_data['id']==7451) {
if(!empty($_POST['wpcf-camera-title'])) {
toolset_connect_posts( 'camera-camera-issue', $_POST['wpcf-camera-title'], $post_id );
}
}
if ($form_data['id']==7451) {
if(!empty($_POST['wpcf-tmna-location'])) {
toolset_connect_posts( 'tmna-location-camera-issue', $_POST['wpcf-tmna-location'], $post_id );
}
}
}
The updated code for the second part would need to be changed to use the correct relationship slug and the order of the parent and child post type in the relationship will need to be changed:
add_action('cred_save_data','func_connect_child_camera_posts',15,2);
function func_connect_child_camera_posts($post_id,$form_data) {
if ($form_data['id']==7451) {
if(!empty($_POST['wpcf-camera-title'])) {
toolset_connect_posts( 'camera-camera-issue', $_POST['wpcf-camera-title'], $post_id );
}
}
if ($form_data['id']==7451) {
if(!empty($_POST['wpcf-vehicle-location'])) {
toolset_connect_posts( 'camera-issue-tmna-location',$post_id, $_POST['wpcf-vehicle-location'] );
}
}
}
Note: In post-relationship, the post type on the left is the parent and the one on the right is the child.
( screenshot: hidden link )
Thanks Waqar, is there a document that would help me explain the differences in the order of relationships between posts and deciding how to choose in these situations?
While creating one-to-one or many-to-many type post-relationships, it doesn't matter which post type you keep on the left parent side and right child side.