最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

multisite - Delete all user's meta without knowing all the 'key'

matteradmin6PV0评论

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
  • "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 Commented Apr 10, 2019 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 Commented Apr 10, 2019 at 10:08
Add a comment  | 

1 Answer 1

Reset to default 2

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

Post a comment

comment list (0)

  1. No comments so far