Skip Navigation

[Resolved] Update Related Post with User Edit form

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 14 replies, has 2 voices.

Last updated by Paul 2 years ago.

Assisted by: Shane.

Author
Posts
#2452937

Hi there,

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

Shane
Supporter

Languages: English (English )

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

Hi Paul,

Thank you for getting in touch.

I'm assuming the code above is for when the user is creating their user profile, it creates a corresponding post with the same information correct?

Also this code works as intended correct?

Please let me know as if this works then we only need to do a few modifications for it to update the user when they update your their profiles.

Thanks,
Shane

#2453763

Hi there,

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.

Many thanks

#2453861

Shane
Supporter

Languages: English (English )

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

Hi Paul,

In this case the code should only require minimal modification.


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

$args = array(
'author'        =>  $current_user_id,
'orderby'       =>  'post_date',
'order'         =>  'ASC',
'post_type' => 'teachers'
);
$posts=get_posts($args);
if(!empty($post)){
foreach($posts as $post){
update_post_meta( $post->id, 'wpcf-first-name', $first_name );
update_post_meta( $post->id, 'wpcf-last-name', $last_name );
update_post_meta(  $post->id, 'wpcf-teacher-email', $user_email );
update_post_meta(  $post->id, 'wpcf-user-id', $post_id );
update_post_meta(  $post->id, 'wpcf-teacher-role', $the_role);    
         
}
}
    }
}

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.

Thanks,
Shane

#2453867

Thanks very much Shane. In fact, I think it was you that wrote the original function!

I will try this and hopefully it will work 🙂 I will let you know shortly.

Thanks again!

Paul

#2453881

Shane
Supporter

Languages: English (English )

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

Hi Paul,

Be mindful that this code will perhaps need some testing and debugging as It was written based on what already existed.

Thanks,
Shane

#2456137

Hey there Shane,

I need a little more help with this one if possible.

Unfortunately, although the form still updates the User details, it's not updating the related teacher post.

$args = array(
'author'        =>  $current_user_id,
'orderby'       =>  'post_date',
'order'         =>  'ASC',
'post_type' => 'teachers'
);
$posts=get_posts($args);
if(!empty($post)){
foreach($posts as $post){
update_post_meta( $post->id, 'wpcf-first-name', $first_name );
update_post_meta( $post->id, 'wpcf-last-name', $last_name );
update_post_meta(  $post->id, 'wpcf-teacher-email', $user_email );
update_post_meta(  $post->id, 'wpcf-user-id', $post_id );
update_post_meta(  $post->id, 'wpcf-teacher-role', $the_role);  

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):

Which is taken here:

$new_title = $first_name . " " . $last_name;
    $new_title = sanitize_text_field( $new_title );

Can you help?

#2456177

I've tried reworking the bottom bit but to no avail:

$posts=get_posts($args);
if(!empty($post)){
foreach($posts as $post){
 $my_post = array(
        'post_title'    => $new_title,
        'post_status'   => 'publish',
        'post_author' => $post_id,
        'post_type' => 'teachers'
      );
	wp_update_post($my_post);
update_post_meta( $post->id, 'wpcf-first-name', $first_name );
update_post_meta( $post->id, 'wpcf-last-name', $last_name );
update_post_meta(  $post->id, 'wpcf-teacher-email', $user_email );
update_post_meta(  $post->id, 'wpcf-user-id', $post_id );
update_post_meta(  $post->id, 'wpcf-teacher-role', $the_role);    
          
}

#2456433

Shane
Supporter

Languages: English (English )

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

Hi Paul,

Would you mind allowing me to have admin access to the website so that I can have a more detailed look at this for you ?

Please where applicable please provide me with a link to an example page where I can see the issue.

I've enabled the private fields for your next response.
Thank,
Shane

#2456441
Screenshot 2022-09-13 at 15-34-38 Edit teacher “Keith Moon” ‹ 360 Skills For Life CIC — WordPress.png

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.

#2457017

Hey there Shane,

Did you have any luck altering the code?

Many thanks

Paul

#2457221

Shane
Supporter

Languages: English (English )

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

Hi Paul,

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?

Thanks,
Shane

#2457379

Hi there,

It's this one:

hidden link

#2457541

Shane
Supporter

Languages: English (English )

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

Hi Paul,

Thank you, this should now be working.

The issue was that I was incorrectly getting the post id with $post->id instead of $post->ID, notice the capitalized ID.

Thanks,
Shane

#2457911

That's fantastic! Thanks very much for your help. Can't beleive it was just down to capitilization 🙂

My issue is resolved now. Thank you!

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