Show Author list in WordPress


In WordPress we can display all users but with default function  we can’t exclude some role. To make that happen, you can use following function. To display just specific user groups or users, there are many template tags available to do so, go to WordPress-Codex – Author Tags.

it’s a simple, not optimized database query. It should be just a basic example and can or must be extend furthermore. Notice, „Registered Readers” are not saved via user_level in the database. This made it necessary to have a separate database query.

You have the possibility to give the function two parameters. To display only one user group you can use the parameter $userlevel. If you need to display first name and last name, then the parameter $show_fullname has to set to TRUE, otherwise it will display the user name only.

To use the function, you have to put it in your functions.php of your theme.

function fb_list_authors($userlevel = 'all', $show_fullname = true) {
global $wpdb;
/*
all = Display all user
1 = subscriber
2 = editor
3 = author
7 = publisher
10 = administrator
*/

if ( $userlevel == 'all' ) {
$author_subscriper = $wpdb->get_results("SELECT * from $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value = 'a:1:{s:10:\"subscriber\";b:1;}'");
foreach ( (array) $author_subscriper as $author ) {
$author = get_userdata( $author->user_id );
$userlevel = $author->wp2_user_level;
$name = $author->nickname;
if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') ) {
$name = "$author->first_name $author->last_name";
}
$link = '<li>' . $name . '</li>';
echo $link;
}
$i = 0;
while ( $i <= 10 ) {
$userlevel = $i;
$authors = $wpdb->get_results("SELECT * from $wpdb->usermeta WHERE meta_key = 'wp_user_level' AND meta_value = '$userlevel'");
foreach ( (array) $authors as $author ) {
$author = get_userdata( $author->user_id );
$userlevel = $author->wp2_user_level;
$name = $author->nickname;
if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') ) {
$name = "$author->first_name $author->last_name";
}
$link = '<li>' . $name . '</li>';
echo $link;
}
$i++;
}
} else {
if ($userlevel == 1) {
$authors = $wpdb->get_results("SELECT * from $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value = 'a:1:{s:10:\"subscriber\";b:1;}'");
} else {
$authors = $wpdb->get_results("SELECT * from $wpdb->usermeta WHERE meta_value = '$userlevel'");
}
foreach ( (array) $authors as $author ) {
$author = get_userdata( $author->user_id );
$userlevel = $author->wp2_user_level;
$name = $author->nickname;
if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') ) {
$name = "$author->first_name $author->last_name";
}
$link = '<li><b>' . $userlevelname[$userlevel] . '</b></li>';
$link .= '<li>' . $name . '</li>';
echo $link;
}
}}

Alternatively you can use a very small syntax. You must customize the fields of the query.

<?php
$wp_user_search = $wpdb->get_results("SELECT ID, display_name FROM $wpdb->users ORDER BY ID");
foreach ( $wp_user_search as $userid ) {
$user_id = (int) $userid->ID;
$user_login = stripslashes($userid->user_login);
$display_name = stripslashes($userid->display_name);
$return = '';
$return .= "\t" . '<li>'. $display_name .'</li>' . "\n";
print($return);
}
 ?>
Advertisements

2 thoughts on “Show Author list in WordPress

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s