1
<?php 
$users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
echo '(', count( get_users( array( 'role' => 'author' ) ) ); echo ')' 
?>

code by Jan Beck

I need to know what is my best option for displaying the total number of authors who have at least posted and/or published something on my blog.

Currently the code is counting all the authors even if they haven't posted anything. Please let me know if there is a simple solution to this. I have looked throughout the wordpress codex and have not found a solution to this yet.

1 Answer 1

1

I believe that wp_list_authors will do what you want, sort of. You could run the function with the echo parameter false and count the results.

$authors = wp_list_authors(
  array(
    'echo'=>false,
    'html'=>false
  )
);
echo var_dump($authors);
echo count (explode(',',$authors));

Or, alternately, and perhaps less profligately, steal that function's SQL.

$authors = $wpdb->get_results(
  "SELECT DISTINCT post_author, COUNT(ID) AS count 
  FROM $wpdb->posts 
  WHERE post_type = 'post' 
  AND " . get_private_posts_cap_sql( 'post' ) . " 
  GROUP BY post_author"
);
var_dump($authors); 
echo count($authors); 

Those are slightly different in that the first ignores the admin user by default, but there is a parameter to change that.

3
  • These are close I can feel it. But I just need the total count eg lets say i have 38 authors but only 28 have posted something i want the count to = 28 not 38... Let me know if that helps. I was thinking a $user_count with hide_empty => true or something to this extent.
    – Joe
    May 21, 2013 at 17:43
  • Man Im not the brightest bulb thats for sure I took the first code and it worked $authors = wp_list_authors( array( 'echo'=>false, 'html'=>false ) ); echo '(', count (explode(',',$authors));echo ')' You ROCK - s_ha_dum Thank you so much saved my but
    – Joe
    May 21, 2013 at 18:12
  • @Joe : if this solved the problem please select it as the "accepted" solution.
    – s_ha_dum
    May 21, 2013 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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