使用WordPress的PHP代码,如何一次仅从数千个用户中批量删除100个订阅者?

时间:2021-03-18 作者:Jessica

使用WordPress PHP代码,如何一次从数千个用户中批量删除100个订阅者?

(以下代码试图一次删除所有50k用户,而我的服务器挂起。如果我一次只能删除100个用户,那么我可以每5分钟使用一次Cron作业。)

<?php
$blogusers = get_users( ‘role=subscriber’ );
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
$user_id = $user->ID;
wp_delete_user( $user_id );
}
谢谢。

2 个回复
最合适的回答,由SO网友:nmr 整理而成

Here 您将找到支持的所有参数get_users().

$blogusers = get_users( [
   \'role\'   => \'subscriber\',
   // limit the number of rows returned
   \'number\' => 100,
] );

foreach ( $blogusers as $user ) {
   wp_delete_user( $user->ID );
}
或仅返回ID:

//  $blogusers is array of IDs
$blogusers = get_users( [
   \'role\'   => \'subscriber\',
   // return only user ID
   \'fields\' => \'ID\',
   // limit the number of rows returned
   \'number\' => 100,
] );

foreach ( $blogusers as $user_id ) {
   wp_delete_user( $user_id );
}

SO网友:techno

您可以使用break 陈述

<?php
$blogusers = get_users( ‘role=subscriber’ );
$i = 0;
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
if(++$i > 100) break;
$user_id = $user->ID;
wp_delete_user( $user_id );
}

相关推荐