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