WordPress不知道是哪个用户创建了哪个,因此首先需要将创建者的数据存储在新创建的用户元中,这样您就可以这样做:
add_action( \'user_register\', \'Store_creator\' );
function Store_creator($user_id){
$creator = wp_get_current_user();
//only do this when none admin creates the user
if ( $creator->roles[0] == \'administrator\' ) return;
update_user_meta($user_id,\'_creator\',$creator->ID);
}
然后,您可以根据该元自定义他可以看到的用户列表:
add_action(\'pre_user_query\',\'custom_pre_user_query\');
function custom_pre_user_query($user_search) {
global $wpdb;
$user = wp_get_current_user();
if ( $user->roles[0] != \'administrator\' ) {
global $wpdb;
$user_search->query_where =
str_replace(\'WHERE 1=1\',
"WHERE 1=1 AND {$wpdb->users}.ID IN (
SELECT wp_usermeta.user_id FROM $wpdb->usermeta
WHERE wp_usermeta.meta_key = \'_creator\'
AND wp_usermeta.meta_value = {$user->ID})",
$user_search->query_where
);
}
}