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.

Please suggest a code snippet to get the total number of authors on a blog who have at least one published article. Thanks

share|improve this question
add comment

1 Answer 1

Hi @rsm:

There are probably better ways, using the WordPress API, but since I know SQL so well this immediately came to mind:

function total_published_authors() {
  global $wpdb;
  $sql =<<<SQL
SELECT COUNT(*) AS total_published_authors FROM (
SELECT DISTINCT
    {$wpdb->users}.ID
FROM {$wpdb->users} 
    INNER JOIN {$wpdb->posts} ON {$wpdb->posts}.post_author={$wpdb->users}.ID
WHERE 
    {$wpdb->posts}.post_status='publish'
GROUP BY 
    {$wpdb->users}.ID
) x
SQL;
  return $wpdb->get_var($sql);
}  

If I come up with a better way I'll post it too.

share|improve this answer
    
Not really on API (as far as I remember), there is wp_list_authors(), but it's large and complex function to generate HTML output from SQL without any intermediary stuff. –  Rarst Jan 31 '11 at 11:02
2  
@Rarst - You know, we really ought to go thorough WPSE and find all the questions that are answered by custom SQL, and build a wrapper around WP_Query() that lets people do the queries they need by passing arguments instead of forcing them to drop into SQL (hint, hint. :) –  MikeSchinkel Jan 31 '11 at 11:12
    
That sounds like a great idea, like a super custom wp_query. –  Bainternet Jan 31 '11 at 11:21
    
@בניית אתרים - Notice the (hint, hint); consider what I told you about via email... –  MikeSchinkel Jan 31 '11 at 11:50
add comment

Your Answer

 
discard

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