[英]How to loop through all users in the database and apply a role
I am trying to create a loop that loops through each users data in my events table in the database and applies the role of suspended if the amount of rows returned is a multiple of 12. 我试图创建一个循环,该循环遍历数据库中事件表中的每个用户数据,并且如果返回的行数是12的倍数,则应用暂停的作用。
Here's what I have created but it doesn't seem to work; 这是我创建的,但是似乎没有用;
<?php
global $wpdb;
for( $x = 1; $x < 300; $x++ ){
$count = $wpdb->get_results( 'SELECT * FROM wp_events WHERE user_id = "'.$x.'"' );
if ($count % 12 == 0){
$user = new WP_User( $x );
$user->add_role( 'suspended' );
}
}
?>
The method get_results
returns an array. 方法get_results
返回一个数组。 Therefore, you are using an array to calculate the modulo, so it's always going to return 1
. 因此,您使用数组来计算模数,因此它将始终返回1
。
To fix this, you can change your condition to: 要解决此问题,您可以将条件更改为:
if (count($count) % 12 == 0) {
// Your code...
}
For more information: https://developer.wordpress.org/reference/classes/wpdb/get_results/ 有关更多信息: https : //developer.wordpress.org/reference/classes/wpdb/get_results/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.