Using WordPress ‘get_nonauthor_user_ids()’ PHP function

The get_nonauthor_user_ids WordPress PHP function retrieves the user IDs of all users who are not authors on your WordPress site.

Usage

To use the get_nonauthor_user_ids function, simply call the function and store the result in a variable. The result will be an array of user IDs.

$non_author_ids = get_nonauthor_user_ids();

Parameters

  • None

More information

See WordPress Developer Resources: get_nonauthor_user_ids
This function is available since WordPress version X.X.X. Check the WordPress Developer Resources for the latest information on this function.

Examples

Display a list of non-author users

This example retrieves non-author user IDs and displays them in a list.

$non_author_ids = get_nonauthor_user_ids();

echo '<ul>';
foreach ($non_author_ids as $user_id) {
    $user_info = get_userdata($user_id);
    echo '<li>' . $user_info->display_name . '</li>';
}
echo '</ul>';

Send an email to non-author users

This example retrieves non-author user IDs and sends an email to each user.

$non_author_ids = get_nonauthor_user_ids();
$subject = "Hello from the website!";
$message = "This is a message for all non-author users.";

foreach ($non_author_ids as $user_id) {
    $user_info = get_userdata($user_id);
    wp_mail($user_info->user_email, $subject, $message);
}

Count the number of non-author users

This example retrieves non-author user IDs and counts the number of users.

$non_author_ids = get_nonauthor_user_ids();
$non_author_count = count($non_author_ids);

echo 'There are ' . $non_author_count . ' non-author users on this website.';

Change the role of non-author users to ‘subscriber’

This example retrieves non-author user IDs and changes their role to ‘subscriber’.

$non_author_ids = get_nonauthor_user_ids();

foreach ($non_author_ids as $user_id) {
    $user = new WP_User($user_id);
    $user->set_role('subscriber');
}

Delete non-author users

This example retrieves non-author user IDs and deletes the users.

$non_author_ids = get_nonauthor_user_ids();

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