Skip Navigation

[Resolved] Connect host to their properties automatically

This thread is resolved. Here is a description of the problem and solution.

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

This support ticket is created 3 years, 11 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1979829

Hi,
I'm creating a real estate web site. Hosts can register themselvers and after registration they can add one or more structure in the website. Now an admin has to enter host page and create manually the relationship between host and structure. I need this connection is automatic. Is it possible?
I tried some solution I found in your forum but those codes don't work for me.
Could you help me to do this?
Thank you.

#1979927

Hello, let me make sure I understand your site setup. It sounds like you have two custom post types: Host and Structure. Each User is the author of one Host post, and the User can create one or more Structure posts. You have a post relationship set up between Host and Structure, and when a User creates a Structure post, you would like to automatically connect the User's Host post and the new Structure post in the post relationship. Is that correct?

Can you tell me:
- The slugs of the Host and Structure post types. You can find out in Toolset > Post Types.
- The slug of the post relationship between Host and Structure. You can find this in Toolset > Relationships.
- Which post type is the "parent" and which post type is the "child" in this relationship? You can find this in Toolset > Relationships. Edit the Host - Structure post relationship, check the checkbox "I understand that changes to these settings may delete post associations in this Relationship", and click "Edit settings".
- Are Structure posts created in wp-admin, or using front-end Forms?

#1980043

Yes the process is right.
The code I'm trying is this:

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 );
}

}

}

Slug are strtuttura and proprietario
Relationship slug is proprietario-struttura
Propietario is parent Struttura is child
Strutture are added from front-end by hosts

Thanks!

#1980045

The custom code you have here looks good at first glance. Perhaps something else is going on.
- What is the status of this User's Proprietario post?
- What is the status of the Struttura post created by the Form?
- Does the Struttura Form contain a post relationship field for the Proprietario - Struttura relationship? If so, please delete the field.
- Can you confirm the Struttura Form ID is 43?

#1980077

Hi Christian... I put the wrong form id... thank you, now it works fine.
I have some other little issues to check but I'll open other tickets.

Thank you for your suggestion.

Have a nice day,
best! Anna.

#1980079

My issue is resolved now. Thank you!