Skip Navigation

[Resolved] Automatically linking related post types

This support ticket is created 4 years, 4 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/Hong_Kong (GMT+08:00)

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 4 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1724907

I found two support articles that discuss ways to automatically link related post types and I started work based upon those instructions but I ran into a problem, When I added the following function:

add_action('cred_save_data', 'connect_tmna_contact_func',10,2);
function connect_tmna_location_func($wpcf-tmna-function-or-area, $form_data)
{
    // if a specific form
    if ($form_data['id']== 35 && isset($_POST['account-number']))
    {
        $account-number = $_POST['account-number'];
        if($account-number){
            toolset_connect_posts(
                $relationship = 'tmna-location',
                $account-number,
                $wpcf-tmna-function-or-area
            );
        }
    }
}

the site goes down.
I'm not entirely sure that I understood this method.

In the above function I'm trying to relate the access contact CPT using it's 'account-number' field to the tmna-location CPT using it's 'tmna-function-or-area' field. But my larger problem is that these two fields are not the same. Do they have to be? IOW, the values are not the same.

Also, I need to relate multiple other CPTs in similar ways, can I reuse this same function? with other fields and forms?

Thanks!

#1725441

Hello,

Your custom PHP codes won't work, all PHP variable name should not contain "-", you can use "_" to replace them.

add_action('cred_save_data', 'connect_tmna_contact_func',10,2);
function connect_tmna_location_func($post_id, $form_data)
{
    // if a specific form
    if ( $form_data['id']== 35 && isset($_POST['account-number']))
    {
        $account_number = $_POST['account-number'];
		$tmna_location = get_post_meta($post_id, 'wpcf-tmna-function-or-area', true);
        if($account_number){
            toolset_connect_posts(
                $relationship = 'tmna-location',
                $account_number,
                $tmna_location
            );
        }
    }
}

And you can customize above PHP function, and setup other post type relationship in same way.

More help:
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