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