Skip Navigation

[Resolved] Copy relationship from Author to child post on CRED SAVE

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

Last updated by Paul 2 years ago.

Assisted by: Shane.

Author
Posts
#2330223

Hi there.

Ticket was closed so have created a new one. I still need help with this if possible?

Basically, when an Author creates a post via a CRED form, I want to copy the same relationship from Author to Child.

I have the code below but it's not working and creating an error. Could you help me please?

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

It's this bit of the code that's causing the error:

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

Shane
Supporter

Languages: English (English )

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

Hi Paul,

The problem with the section below is because the get_posts() function will return an array of posts

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

You will need to structure it like this.

//setup relationship for teachers
$args = array(
  'post_author' => $current_user_id
);
$current_user_profiles =  get_posts($args);
foreach($current_user_profiles as $current_user_profile){
$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 try this and let me know the results.

Thanks,
Shane

#2330411

Hi Shane,

It's still throwing a critical error.

The Teacher is assigned to a school.
I've logged in as that teacher and using the form to add another teacher.
Throwing a critical error. Here is my actual code. I note from my other ticket regarding this, you mentioned the error_log. Where would i find this?

//setup relationship for teachers
$args = array(
  'post_author' => $current_user_id
);
$current_user_profiles =  get_posts($args);
		error_log('this is my current user post'.$current_user_profile->ID);
foreach($current_user_profiles as $current_user_profile){
$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);
    }
}
    }

#2330883

Shane
Supporter

Languages: English (English )

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

Hi Paul,

The error_log() function will dump the error in your php log files on your server.

Perhaps I need access to this particular site to craft the code a little better for you.
I've enabled the private field for your next response.

Also please let me know the page that the form is on.

Thanks,
Shane

#2331137

Shane
Supporter

Languages: English (English )

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

Hi Paul,

After doing some tests and debugging of the code I was able to get it to work.

Your teachers should now be successfully connecting to the same parent of the user who created their profiles.

Thanks,
Shane

#2331677

My friend, I cannot thank you enough. That's above and beyond 5* support. You are totally awesome. Thank you so very much for the help.

Sharing the final code for others....

// Shane - Add relationship between author and child (School Lead and new teacher). Thanks for your help!
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 ) ) {
    {
		$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' ); //replace writer with parent post type slug.
//connect the current post being created to the related post.
toolset_connect_posts( 'school-teacher', $current_user_related_post, $new_post_id);
    }
	}
}
    }
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.