如何在WordPress中批量删除特定用户?

How to delete specific users in bulk in WordPress?

user_id 已知时,WordPress 如何批量删除用户?

有没有简单的方法来做到这一点?例如 SQL CLIPHP CLI 或类似的东西。有人做过吗?

尝试使用以下代码片段删除 ID 已知的特定用户。

function delete_specific_users() {

  //Include the user file with the user administration API
  require_once( ABSPATH . 'wp-admin/includes/user.php' );

  //Get a list of users that belongs to the specified role
  $users = get_users( array( 'ID' => array( comma separated list of user_ids you want to delete ) ) );

  //Delete all the user of the specified role
  foreach ( $users as $user ) {
      wp_delete_user( $user->ID );
  }

}

// call the method like this.
delete_specific_users();

使用 WordPress CLI。有一个可以接受列表的用户删除命令。

文档中的示例:

# Delete user 123 and reassign posts to user 567
$ wp user delete 123 --reassign=567
Success: Removed user 123 from http://example.com

# Delete all contributors and reassign their posts to user 2
$ wp user delete $(wp user list --role=contributor --field=ID) --reassign=2

文档:https://developer.wordpress.org/cli/commands/user/delete/