Custom Query for Top Authors

Query and display top authors based on the number of posts.

<?php
function display_top_authors() {
    $args = array(
        'orderby' => 'post_count',
        'order' => 'DESC',
        'number' => 5
    );
    $authors = get_users($args);

    echo '<ul class="top-authors">';
    foreach ($authors as $author) {
        echo '<li><a href="' . get_author_posts_url($author->ID) . '">' . esc_html($author->display_name) . ' (' . $author->post_count . ' posts)</a></li>';
    }
    echo '</ul>';
}
add_shortcode('top_authors', 'display_top_authors');
?>

Post Comment