admin管理员组文章数量:1303327
Using WordPress PHP code, how to bulk delete ONLY 100 subscribers at a time from thousands of users?
(The following code tries to delete all 50k users at once and my server hangs. If I can delete only 100 users at a time then I can use a Cron job every 5 minutes.)
<?php
$blogusers = get_users( ‘role=subscriber’ );
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
$user_id = $user->ID;
wp_delete_user( $user_id );
}
Thanks.
Using WordPress PHP code, how to bulk delete ONLY 100 subscribers at a time from thousands of users?
(The following code tries to delete all 50k users at once and my server hangs. If I can delete only 100 users at a time then I can use a Cron job every 5 minutes.)
<?php
$blogusers = get_users( ‘role=subscriber’ );
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
$user_id = $user->ID;
wp_delete_user( $user_id );
}
Thanks.
Share Improve this question asked Mar 18, 2021 at 4:55 JessicaJessica 32 bronze badges2 Answers
Reset to default 1Here you will find all parameters supported by get_users()
.
$blogusers = get_users( [
'role' => 'subscriber',
// limit the number of rows returned
'number' => 100,
] );
foreach ( $blogusers as $user ) {
wp_delete_user( $user->ID );
}
Or return only 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 );
}
You can use break
statement
<?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 );
}
本文标签:
版权声明:本文标题:plugin development - Using WordPress PHP code, how to bulk delete only 100 subscribers at a time from thousands of users? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741672061a2391662.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论