0

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() );            
    }
}
  • "But is there then a way to have it get the wp_ table prefix." - yes, you can get the prefix or the complete table names from the $wpdb object. – Rup Apr 10 at 10:06
  • You can get all metadata keys that exist for a user with 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 Apr 10 at 10:08
1

No 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

  • thank you! didn't realise it was that way - makes it easier – markb Apr 11 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.