How to List the Top 5 Authors by Number of Posts in WordPress

Home » Snippets » How to List the Top 5 Authors by Number of Posts in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Keeping track of your site’s content production can sometimes be a daunting task, especially when multiple authors are involved. Among the various ways of managing this, one effective measure can be tallying the number of posts by each author. It not only helps maintain a balance, but also encourages a healthy, competitive spirit among authors. If you’re curious about how you can compile a list of your site’s top 5 authors based upon the number of posts they’ve contributed, you’ve come to the right place. In this article, we will be guiding you through the process with easy-to-follow steps.

					function wpturbo_list_top_authors() {

    $authors = get_users(array( 'orderby' => 'post_count', 'order' => 'DESC', 'number' => 5, 'who' => 'authors' ));

    foreach ($authors as $author) {
        echo '<p>';
        echo 'Author: ', $author->display_name, ' - Posts: ', count_user_posts($author->ID);
        echo '</p>';
    }

}
add_action( 'wp_footer', 'wpturbo_list_top_authors' );
				

In the given snippet, we begin by defining a function called wpturbo_list_top_authors(). This function is responsible for retrieving and displaying the top five authors by their post count on your WordPress website.

Inside the function, our goal is to retrieve a list of author users ordered by their post count. We achieve this by using the get_users() function coupled with certain parameters.

$authors = get_users(array( 'orderby' => 'post_count', 'order' => 'DESC', 'number' => 5, 'who' => 'authors' ));

In the parameter array of the get_users() function:

  • The ‘orderby’ => ‘post_count’ statement instructs the get_users() function to return the users ordered by how many posts they have made.
  • The ‘order’ => ‘DESC’ statement further specifies that the order of the returned users should be in descending order, meaning the author with the most posts will be at the beginning of the array.
  • The ‘number’ => 5 statement restricts the number of returned authors to five, and
  • ‘who’ => ‘authors’ ensures that we only retrieve users who have the role of authors.

Once we’ve retrieved the list of authors, we loop through the list using a foreach loop.

Within the loop:

foreach ($authors as $author) {
    echo '<p>';
    echo 'Author: ', $author->display_name, ' - Posts: ', count_user_posts($author->ID);
    echo '</p>';
}

We display each author’s name (using $author->display_name) along with their number of posts (using count_user_posts($author->ID)). Both pieces of information are enclosed in a paragraph (<p>) for easy readability.

Finally, we use the add_action() function to hook the wpturbo_list_top_authors() function into the ‘wp_footer’ action. Because we are hooking into the ‘wp_footer’ action, the list of authors will be displayed in the footer of every page.

add_action( 'wp_footer', 'wpturbo_list_top_authors' );

Remember that if you want this function to execute on a different area of your site, you’ll need to hook it into the appropriate action.

Register an account to save your snippets or go Pro to get more features.