Skip Navigation

[Resolved] Automatically create relationship based on user

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by Chris 4 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1805133

Tell us what you are trying to do?
Each user has a Profile custom post with their full details.
Users can create an Exhibition (another custom post type) for listing.
When displaying the Exhibitions to the public, I want it to display info from the Profile of the Author.
I think I can do it using a Relationship, but each user needs to link each Exhibition as they create it to their Profile.
I want it to do this automatically based on the Author of the Exhibition post.

Is this possible?

What is the link to your site?
hidden link

#1805887

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

What if you create a one-to-many post relationship between your post type "Profile" which holds your user profile information and post type "Exhibition".

Now, when user create a new Exhibition, you should link the Exhibition post with the user profile post.

If you can share access details and tell me where you added the form using which user can create Exhibition post and send me access details of admin user as well as frontend user using which I should create Exhibition post using Toolset form I will review your setup and make the necessary adjustment or if you know how to do it, you should go ahead and apply such changes to your form.

Please let me know if you will require further assistance.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1807989

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for sharing all required information.

I've added the Toolset form's hook "cred_save_data" that will allow use to find the current loggedin user profile post and using Toolset Post-relationship API function: toolset_connect_posts() we will conntect the profile post with the Exhibition post.

I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link

add_action('cred_save_data', 'func_connect_profile_with_exhibition',10,2);
function func_connect_profile_with_exhibition($post_id, $form_data){

  if ($form_data['id']==1276){
  			$current_user_id = get_current_user_id();
  			$args = array(
						'author'        =>  $current_user_id,
						'post_type'	=> 'profile',
						'fields' => "ids");

			$found_profile = get_posts($args);
            if(count($found_profile) > 0){
    			toolset_connect_posts( 'profile-exhibition',$found_profile[0], $post_id );
            }
        
  }

}

More info:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
=> 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

Can you please confirm it works as expected.

#1810847

I'm not entirely sure how this works, but my issue is resolved now. Thank you!
Hopefully this sort of thing can be done in future versions without coding...

#1839567

Hi. I am trying to do this same thing with another custom post type. I have duplicated the custom code as below but it isn't working. Any idea where I have gone wrong?
I have updated the function name, form ID and the toolset_connect_posts name
thanks

add_action('cred_save_data', 'func_connect_profile_with_report',10,2);
function func_connect_profile_with_report($post_id, $form_data){

if ($form_data['id']==15800){
$current_user_id = get_current_user_id();
$args = array(
'author' => $current_user_id,
'post_type' => 'profile',
'fields' => "ids");

$found_profile = get_posts($args);
if(count($found_profile) > 0){
toolset_connect_posts( 'profile-country-report',$found_profile[0], $post_id );
}

}

}