wp_get_users_with_no_role() WordPress Function

The wp_get_users_with_no_role() function can be used to get a list of users with no role assigned to them. This can be useful for finding out which users have not been assigned a role, or for troubleshooting role-related issues.

wp_get_users_with_no_role( int|null $site_id = null ) #

Gets the user IDs of all users with no role on this site.


Parameters

$site_id

(int|null)(Optional) The site ID to get users with no role for. Defaults to the current site.

Default value: null


Top ↑

Return

(string[]) Array of user IDs as strings.


Top ↑

Source

File: wp-includes/user.php

function wp_get_users_with_no_role( $site_id = null ) {
	global $wpdb;

	if ( ! $site_id ) {
		$site_id = get_current_blog_id();
	}

	$prefix = $wpdb->get_blog_prefix( $site_id );

	if ( is_multisite() && get_current_blog_id() != $site_id ) {
		switch_to_blog( $site_id );
		$role_names = wp_roles()->get_names();
		restore_current_blog();
	} else {
		$role_names = wp_roles()->get_names();
	}

	$regex = implode( '|', array_keys( $role_names ) );
	$regex = preg_replace( '/[^a-zA-Z_\|-]/', '', $regex );
	$users = $wpdb->get_col(
		$wpdb->prepare(
			"
		SELECT user_id
		FROM $wpdb->usermeta
		WHERE meta_key = '{$prefix}capabilities'
		AND meta_value NOT REGEXP %s
	",
			$regex
		)
	);

	return $users;
}


Top ↑

Changelog

Changelog
VersionDescription
4.9.0The $site_id parameter was added to support multisite.
4.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More