I have developed a small wordpress application where It has Institutes(wp user), Trainers(wp user), Trainees(wp user), courses(custom post) and notifications(custom post). All the application is working fine but If I delete an institute all the information belongs to that institute like Trainers, Trainee, notifications & courses should also be deleted. While I was creating the 'institute' user I am storing the all the related info like first name, last name in wordpress table 'wp_users' and 'institute name' is storing as 'user meta' with key 'inistitute_name' in 'wp_usermeta' table like below is my code:
$user_data = array(
'ID' => '',
'user_pass' => '',
'user_login' => $first_name,
'user_email' => $user_email,
'first_name' => $first_name,
'last_name' => $last_name,
'role' => 'admin'//get_option('default_option')
);
$random_password = wp_generate_password(8,false);
$user_id = wp_insert_user( $user_data );
update_user_meta( $user_id, 'inistitute_name',$insititute_name );
While creating Trainer (or) Trainee my code is like below:
$user_data = array(
'ID' => '',
'user_pass' => '',
'user_login' => $first_name,
'user_email' => $trainer_email,
'first_name' => $first_name,
'last_name' => $last_name,
'role' => 'trainer'
);
$random_password = wp_generate_password(8,false);
$user_id = wp_insert_user( $user_data );
update_user_meta( $user_id, 'inistitute_name',$this->institute_name[0] );
wp_set_password($random_password, $user_id);
While creating courses my code is like below:
$user_ID = get_current_user_id();
$institute_name = get_user_meta($user_ID, 'inistitute_name', true);
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_status' => 'publish',
'post_type' => "courses"
);
$id = wp_insert_post($post);
update_post_meta( $id, 'inistitute_name', $institute_name );
Suppose let us say if a institute 'abcd' is deleted then all the information like 'trainer', 'trainee', 'courses' & 'notifications' associated with that institute should also be deleted based on a 'user_meta' field. Is it possible to delete? (or) did I do any mistake? can anyone tell me what was I doing wrong?
Update:
add_action( 'delete_user', 'cyb_delete_institute_info' );
function cyb_delete_institute_info( $id ) {
$user = get_userdata( $id );
if( ! empty( $user ) && in_array( "admin", (array) $user->roles ) ) {
// Deleted user is admin, check `institute_name` meta field
$institute_name = get_user_meta( $id, 'inistitute_name', true );
if( ! empty( $institute_name ) ) {
// Deleted user is an admin and has `institute_name` meta,
// so, it is a "Institute" user
// Get posts to delete
$posts_args = array(
'post_type' => 'notifications',
'meta_query' => array(
'key' => 'inistitute_name',
'value' => $institute_name
),
);
global $wpdb;
$querystr = "DELETE FROM $wpdb->wp_dxwe_posts LEFT JOIN $wpdb->wp_dxwe_postmeta ON $wpdb->wp_dxwe_postmeta.post_id = $wpdb->posts.ID WHERE $wpdb->wp_dxwe_postmeta.meta_key = 'inistitute_name' AND
$wpdb->wp_dxwe_postmeta.meta_value = '$institute_name' AND $wpdb->wp_dxwe_posts.post_type = 'courses' ";
$wpdb->query( $querystr );
$posts_args2 = array(
'post_type' => 'courses',
'meta_query' => array(
'key' => 'inistitute_name',
'value' => $institute_name
),
);
$query = new WP_Query( $posts_args );
$query2 = new WP_Query( $posts_args2 );
$posts_to_delete = $query->get_posts();
$posts_to_delete2 = $query2->get_posts();
foreach( $posts_to_delete as $post ) {
wp_delete_post( $post->ID );
}
foreach( $posts_to_delete2 as $post ) {
wp_delete_post( $post->ID );
}
// Get users to delete
$users_args = array(
'role__in' => array( 'trainer', 'trainee' ),
'meta_query' => array(
'key' => 'inistitute_name',
'value' => $institute_name
),
);
$query = new WP_User_Query( $users_args );
$users_to_delete = $query->get_results();
foreach( $users_to_delete as $user ) {
wp_delete_user( $user->ID );
}
}
}
}
I was writing 2 separate WP_Query's to delete custom post_type's separately but din't work either. same result only notifications deleting correctly but for courses other institute courses also getting deleted.
institute_name
meta field? I think you should assign "institute" a user role as well, but that is another question; working on an answer.