Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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"
);  
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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:

$args = array(
    'role'         => 'author',
    'orderby'      => 'display_name'
);
$authors = get_users( $args );

[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).

share|improve this answer
    
What about ordering it by the number of posts? –  MidhuN Apr 5 '13 at 13:55
    
@MidhuN See revised answer. –  Johannes Pille Apr 5 '13 at 13:58
    
Thank you @Johannes Pille –  MidhuN Apr 5 '13 at 14:01
add comment
$args = array();
$args['fields'] = array( 'ID', 'display_name' );
$args['role'] = 'author';
$authors= get_users( $args );

Reference : http://codex.wordpress.org/Function_Reference/get_users

share|improve this answer
    
What about ordering it by the number of posts? –  MidhuN Apr 5 '13 at 13:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.