0

Hopefully quite an easy question for you code-savvy ladies and gents today: how would one go about echoing the precise number of registered users on a WordPress blog whom have successfully published at least one post (discounting pending, drafts, etc.)? Subscribers are able to publish posts on my blog, and there's a terrific number of new members joining and new posts being generated daily, so manually updating the number would be a bit tricky (hehe).

I'd love to be able to just plonk a somewhere in the sidebar, but, given my limited knowledge of PHP, I'm at my wit's end as to how to accomplish this.

Thanks for any and all help, guys. Cheers!

0

Here is one I happen to use myself. The function returns an array consisting of an array of author ID's, an array of author names and the number of authors.

Place this code in functions.php -

/**
 * Return an array of the number of posting authors, and their names and ID's
 */
function get_author_data(){

    global $wpdb;

    /** Create the query and then grab all of the ID's of all authors who have posted at least once */
    $query = $wpdb->prepare('SELECT DISTINCT %1$s.post_author FROM %1$s WHERE %1$s.post_status IN ("publish")', $wpdb->posts);
    $author_ids = $wpdb->get_col($query);
    $authors['ID'] = $author_ids;

    /** Get the name of all of the authors that have posted at least once */
    $authors['name'] = array();
    if(!empty($authors['ID'])) : foreach($authors['ID'] as $author_id) :

            $query = $wpdb->prepare('SELECT %1$s.display_name FROM %1$s WHERE %1$s.ID = %2$s', $wpdb->users, $author_id);
            $authors['name'][] = $wpdb->get_var($query);

        endforeach;
    endif;

    /** Get a count of the number of authors who have posted at least once */
    $authors['count'] = count($authors['ID']);

    return $authors;

}

When you wish to grab the information -

$authors = get_author_data();

And then to output the information that you require -

/** Output all author names */
if(!empty($authors['name'])) : foreach($authors['name'] as $author_nam) :

        print_f('<li>%1$s</li>', $author_name);

    endforeach;
endif;

/** Output a count of authors */
print_f('<p>Number of contributing authors: %1$s</p>', $authors['count']);
  • Cheers for the swift response mate! Apologies for my ignorance, but how would one go about echoing that number in the sidebar? <?php echo $authors; ?> and <?php echo $authors['count']; ?> just returns an error. I don't doubt that I'm just attempting to echo the wrong thing - I'm really very new to all this! :p – Steve Mar 20 '13 at 12:03
  • It sounds like you are not calling the function before trying to output the results. I've edited my answer to show you how to call it and given a couple of examples of how you can output the results. – David Gard Mar 20 '13 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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