Because “institute” is a user and you want to perform some actions when it is deleted, you could use delete_user
action (as we need information of the deleted user, deleted_user
may not work, not sure about this but delete_user
should work for sure). In that hook you can get all users and posts based on inistitute_name
meta field and delete them.
Seeing your code, “Institute” user type can be identified by user role (you assing admin
) and user meta inistitute_name
. So, in delete_user
action you can check if the deleted user is and admin and if it has institute_name
assigned, in that case, the deleted user is a “institute” and you can get all other associated users and posts.
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 `inistitute_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' => array( 'courses', 'notifications' )
'meta_query' => array(
'key' => 'inistitute_name',
'value' => $institute_name
),
);
$query = new WP_Query( $posts_args );
$posts_to_delete = $query->get_posts();
foreach( $posts_to_delete 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 );
}
}
}
}
Note: not tested, just written here as sample code.