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!
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