Can anyone tell me how to get all the users who have author privileges by querying the DB in WordPress and order them by number of posts written by them.
I use the following query to get all the users from DB:
$authors = $wpdb->get_results(
"SELECT ID, user_nicename from $wpdb->users ORDER BY display_name"
);
Unless you want to retrieve custom data from the database, you will hardly ever need to make use of the
WPDB
class (or its global object, respectively). Though it is obviously possible to do things that way as well.Just for the sake of completeness, if you had a reason to not use a more abstract function, you’d have to employ
JOIN
in the select syntax as user roles live in the usermeta table.But it’d be much simpler to make use of the
get_users
function:[EDIT] As for the question in the comments (ordering by the number of posts):
Use
'post_count'
as an argument for the'orderby'
parameter (and read the linked codex page, which gives you all the possible arguments).Reference : http://codex.wordpress.org/Function_Reference/get_users