Generate a user list per site to communicate upgrade plans

This should do the trick. Read along with the comments for some explanation.

// get users with specified roles -- this can go in functions
function get_users_with_role( $roles ) {
    global $wpdb;
    if ( ! is_array( $roles ) )
        $roles = array_walk( explode( ",", $roles ), 'trim' );
    $sql="
        SELECT  ID 
        FROM        " . $wpdb->users . ' INNER JOIN ' . $wpdb->usermeta . '
        ON          ' . $wpdb->users . '.ID             =       ' . $wpdb->usermeta . '.user_id
        WHERE       ' . $wpdb->usermeta . '.meta_key        =       \'' . $wpdb->prefix . 'capabilities\'
        AND     (
    ';
    $i = 1;
    foreach ( $roles as $role ) {
        $sql .= ' ' . $wpdb->usermeta . '.meta_value    LIKE    \'%"' . $role . '"%\' ';
        if ( $i < count( $roles ) ) $sql .= ' OR ';
        $i++;
    }
    $sql .= ' ) ';
    $sql .= ' ORDER BY display_name ';
    $userIDs = $wpdb->get_col( $sql );
    return $userIDs;
}

////// everything else could go in a custom page template just for viewing temporarily.

// poll database for users we need, using custom function (listed above)
$editors_and_admins = get_users_with_role(array('editor', 'administrator'));

// get user objects
$editors_and_admins = get_users(array('include' => $editors_and_admins);

echo '<table>';
// spit out as table - not sure what output you need. could easily create CSV by modifying this
foreach($editors_and_admins as $constituent){
    echo '<tr>'
    // get name
    echo '<td>'.get_the_author_meta('first_name', $constituent->ID).' '.get_the_author_meta('last_name', $constituent->ID).'</td>';
    // get email
    echo '<td><a href="https://wordpress.stackexchange.com/questions/101043/mailto:".$constituent->user_email'">'.$constituent->user_email.'</a></td>';
    // get URL
    echo '<td><a href="'.$constituent->user_url.'">'.$constituent->user_url.'</a></td>';
    echo '</tr>';
}
echo '</table>';

Leave a Comment