Problem: I have two post types - Hosts and Structures - in a post relationship. Each User can be the author of one Host post, and can create multiple Structure posts using Forms. When the User submits a Form to create one of these Structure posts, I would like to automatically connect it to the User's Host post.
Solution: Use the cred_save_data API to trigger custom code when the Structure post is created. Use the toolset_connect_posts API to link the two posts programmatically.
add_action('cred_save_data', 'func_connect_host_with_property',10,2);
function func_connect_host_with_property($post_id, $form_data){
if ($form_data['id']==43){
$current_user_id = get_current_user_id();
$args = array(
'author' => $current_user_id,
'post_type' => 'proprietario',
'fields' => "ids");
$found_profile = get_posts($args);
if(count($found_profile) > 0){
toolset_connect_posts( 'proprietario-struttura',$found_profile[0], $post_id );
}
}
}
Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data