Trying to delete users from the frontend - which I currently have working. Previously, I was manually deleting the metadata when it was completed.
I was looking to see if it was possible to delete all associated to the now non-existent user ID.
I tried finding anything about looping the delete_user_meta($user_id, *)
but couldn't with how to match the key.
I found this:
DELETE FROM wp_usermeta WHERE NOT EXISTS ( SELECT * FROM wp_users WHERE wp_usermeta.user_id = wp_users.ID )
But is there then a way to have it get the wp_
table prefix.
This is the way I am deleting from the frontend after the $_POST and validation:
if( $mb_user_delete ) {
require_once( ABSPATH . 'wp-admin/includes/user.php' );
$user_delete_id = wp_delete_user( $user_id );
if( !is_wp_error($user_delete_id) ) {
delete_user_meta( $user_id, '' );
die( 'User has been successfully deleted' );
} else {
die( $user_delete_id->get_error_message() );
}
}
Trying to delete users from the frontend - which I currently have working. Previously, I was manually deleting the metadata when it was completed.
I was looking to see if it was possible to delete all associated to the now non-existent user ID.
I tried finding anything about looping the delete_user_meta($user_id, *)
but couldn't with how to match the key.
I found this:
DELETE FROM wp_usermeta WHERE NOT EXISTS ( SELECT * FROM wp_users WHERE wp_usermeta.user_id = wp_users.ID )
But is there then a way to have it get the wp_
table prefix.
This is the way I am deleting from the frontend after the $_POST and validation:
if( $mb_user_delete ) {
require_once( ABSPATH . 'wp-admin/includes/user.php' );
$user_delete_id = wp_delete_user( $user_id );
if( !is_wp_error($user_delete_id) ) {
delete_user_meta( $user_id, '' );
die( 'User has been successfully deleted' );
} else {
die( $user_delete_id->get_error_message() );
}
}
Share
Improve this question
asked Apr 10, 2019 at 10:03
markbmarkb
2996 silver badges18 bronze badges
2
|
1 Answer
Reset to default 2No need to find and delete user meta explicitly, because when you remove a WordPress user using wp_delete_user()
, it deletes user and, Posts and all meta is also be deleted that are for that User ID..
Documentation
get_metadata
. But I'd be surprised if you needed to: I'd have expected wp_usermeta to FKR back to the user table. If the user still exists there but is flagged deleted why do you need to delete the metadata? – Rup Commented Apr 10, 2019 at 10:08