Skip Navigation

[Resolved] Create relationship in CRED cred_save_data

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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 2 voices.

Last updated by Shane 2 years ago.

Assisted by: Shane.

Author
Posts
#2322787

Hi there. I wondered if you could help?

1. My website is for a school. We have Schools & Teachers setup as CPT's
2. We have a relationship: Many teachers can be assigend to One School (One To Many)
3. We have a User form which allows one teacher to add further teachers under their school.
4. The teacher adding has already been linked with a school through a relationship form.
5. I use the code below so that a new Teacher CPT gets added when the "New Teacher" User form is used:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
	  $form_id = array( 13915,13246,14273,13916,15076,15530 );
	 if ( in_array( $form_data['id'], $form_id ) ) {
    {
	$first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
	$user_email = $_POST['user_email'];	
	$the_role = get_user_meta( $post_id, 'user_role', true );
	$new_title = $first_name . " " . $last_name;
    $new_title = sanitize_text_field( $new_title );
      $my_post = array(
        'post_title'    => $new_title,
        'post_status'   => 'publish',
        'post_author' => $post_id,
        'post_type' => 'teachers'
      );
          
      // Insert the post into the database
      // Insert the post into the database and save new Client post ID
$new_post_id = wp_insert_post( $my_post );
    }
}
	}

What I need help with is that I want to automatically assign a new Teacher the same connection with a school that the current teacher adding them has been assigned with.

I have the clue that it might be adding the following lines to the above but it's as far as I've got, I just need to connect the new teacher to the parent teachers school somehow:

add_action('cred_save_data', 'add_school_lead_to_teacher',10,2);
function add_school_lead_to_teacher($user_id, $form_data)
{
$current_user_id = get_current_user_id();
 
    // if a specific form
    if ($form_data['id']==15076)
    {
        if (!empty($current_user_id))
        {
 
        }
    }
}

Any help much appreciated!

#2323067

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Paul,

Thank you for getting in touch.

I believe I understand what you want to achieve here.

My question here is that how is a teacher linked to their teacher profile in the teacher cpt. Given that the teachers aren't necessarily the authors of their profile post.

Will it be a case where the teachers are only linked to their profiles by the Post Titles? Either way the functions you should be looking at are the ones below.
To get the related post:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

To connect posts in a relationship:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

Thanks,
Shane

#2323125

Hi Shane,

To explain further:

1. When a user is created, with an Add User form, a CPT of Teacher is created of which they are the author.
2. Now, when this teacher creates another user, The code I supplied grabs the new user ID and assigns the new user as the author of the new Teacher CPT:

 'post_author' => $post_id,

3. I need to grab the current user ID (the one adding the new User) > find out what school they are connected with > apply this same connection to the new user they are adding.

Does that make more sense? I'm just struggling with the function code / modifying that chunk of code I supplied to just apply the above at the same time....

Many thanks

#2323169

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Paul,

So this code here is creating the teachers post. The connection will need to be done on this form itself.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
     $current_user_id = get_current_user_id(); // get ID of current user creating the new teacher.

      $form_id = array( 13915,13246,14273,13916,15076,15530 );
     if ( in_array( $form_data['id'], $form_id ) ) {
    {
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $user_email = $_POST['user_email']; 
    $the_role = get_user_meta( $post_id, 'user_role', true );
    $new_title = $first_name . " " . $last_name;
    $new_title = sanitize_text_field( $new_title );
      $my_post = array(
        'post_title'    => $new_title,
        'post_status'   => 'publish',
        'post_author' => $post_id,
        'post_type' => 'teachers'
      );
           
      // Insert the post into the database
      // Insert the post into the database and save new Client post ID
$new_post_id = wp_insert_post( $my_post );

//setup relationship for teachers
$args = array(
  'post_author' => $current_user_id
);
$current_user_profile =  get_posts($args);
$current_user_related_post = toolset_get_related_post( $current_user_profile->ID, array( 'writer', 'teachers' ) ); //replace writer with parent post type slug.

//connect the current post being created to the related post.
toolset_connect_posts( 'relationship-slug', $new_post_id, $current_user_related_post->ID);
    }
}
    }

PLease let me know if the above helps. There might be some errors and debugging needed for it to work but essentially this is the right track that you should be following.

Thanks,
Shane

#2323939

Hi there Shane,

Thank you for your help but unfortunately, on submitting the form, it's throwing a critical error. It does the rest like creating the new user and new Teacher CPT but doesn't connect it to the current users school

$args = array(
  'post_author' => $current_user_id
);
$current_user_profile =  get_posts($args);
$current_user_related_post = toolset_get_related_post( $current_user_profile->ID, array( 'school', 'teachers' ) ); //replace writer with parent post type slug.
 
//connect the current post being created to the related post.
toolset_connect_posts( 'school-teacher', $new_post_id, $current_user_related_post->ID);
#2323965

Seems to be this bit of the code causing an issue:

$current_user_related_post = toolset_get_related_post( $current_user_profile->ID, array( 'school', 'teachers' ) ); 
#2324069

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Paul,

As mentioned this was just a rough outline of what needs to be done so I expected that there would've been errors. Now debugging needs to be done to pinpoint why it is throwing an error.

Perhaps we need to trace the code below to see if we are getting the correct variable.

What I would do is to intentionally dump the variable value to your php logs to see if we are getting the actual value.

Try adding this line.

$current_user_profile =  get_posts($args);
error_log('this is my current user post'.$current_user_profile->ID);

This should add a log in your server's php logs with the message and the ID of the user who is currently logged in post id.

Please let me know if this helps.
Thanks,
Shane

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.