Skip Navigation

[Resolved] Using the toolset_connect_posts function

This support ticket is created 4 years, 3 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 9 replies, has 2 voices.

Last updated by larryL 4 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#1770043

I added a function:

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?

#1770097

Question about this:


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?

#1770867

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.

#1771575

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

Thanks

#1772953

As explained in the other thread ( ref: https://toolset.com/forums/topic/the-count-is-not-showing-through-wpv-items-count-shortcode/#post-1772091 ), the website is still showing the "critical error" message.

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.

#1773867

Sorry, typo in a function. It's back up.

#1776047

Thanks for writing back.

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 )

#1778413

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?

#1778527

To better understand the post-relationships, you'll find this guide useful:
https://toolset.com/course-chapter/using-post-relationships-on-directory-sites/

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.

What important is that once you've created a post-relationship and are using a function like "toolset_connect_posts" ( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts ), you'll need to provide the arguments in the correct order, i.e. slug of the relationship, parent post's ID & child post's ID.

#1779667

My issue is resolved now. Thank you!