I have a function that creates a related post when a new user is added (Please see code below).
My problem is, I have a CRED form that allows the User to edit their details (including email address). I have a CRED User form "Edit User Details". However, this only edits the User profile and not the related post that was created when they created their New User.
Can you point me in the right direction of creating a CRED Save Data function when the User Edit form gets saved that will also update the related post? (We need the related post as we need a frontend way of searching - which listing users in a VIEW form does not allow).
Hope this makes sense. See below:
// Add relationship between author and child (School Lead and new teacher).
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
$form_id = array( 15076 );
if ( in_array( $form_data['id'], $form_id ) ) {
{
$current_user_id = get_current_user_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 );
// Update any custom fields on Client Post
update_post_meta( $new_post_id, 'wpcf-first-name', $first_name );
update_post_meta( $new_post_id, 'wpcf-last-name', $last_name );
update_post_meta( $new_post_id, 'wpcf-teacher-email', $user_email );
update_post_meta( $new_post_id, 'wpcf-user-id', $post_id );
update_post_meta( $new_post_id, 'wpcf-teacher-role', $the_role);
//setup relationship for teachers
$args = array(
'author' => $current_user_id,
'post_type' => 'teachers'
);
$current_user_profiles = get_posts($args);
if(!empty($current_user_profiles)){
$current_user_related_post = toolset_get_related_post( $current_user_profiles[0]->ID, "school-teacher",'parent' );
//connect the current post being created to the related post.
toolset_connect_posts( 'school-teacher', $current_user_related_post, $new_post_id);
}
}
}
}
Thanks for getting back to me! Yes this code works perfectly and when a user is registered it also creates a corresponding Teacher Custom Post with the same details (used for frontend searches etc).
As mentioned, I need a function for an "Edit User" form which will, on save, also update the linked Custom Post with the same details. At the moment, when they edit their user details (email / name) it means the linked post disappears from the frontend.
Please try the above code and let me know if it helps. Of Course you will need to adjust the ID for the form. This is the basic skeleton of what's needed when updating the post.
Could it be down to the User ID / Author ID? When I check the Author ID it is the same as the User ID in the backend. I'm wondering if it's just not finding the correct post.
(Also btw, I need to update the Post Title in the above code):
One further thing - I do have a CPT field that adds the USER ID to the Teacher post (like a post reference field). Perhaps we could use this.
One other thing is that the code must ONLY edit ONE Teacher post (the one created when the User was first added)....as they also have the ability to add further users (and therefor teachers) after they have logged in. Hence the post_date ASC but need to just return the earliest post.
Thanks for the login details. I'm trying to check on this now for you. However I need to find the existing user profile from the list below. hidden link
Can you let me know which profile is for this sample user you've sent?