# Author Leaderboard Widget

> Adds a dashboard widget ranking authors by the number of posts they published this month, with last month's figure for comparison, to celebrate your most active writers.

- Type: PHP
- Runs: Admin only
- Category: [Dashboard](https://wpide.com/library/category/dashboard/)
- Tags: dashboard-widget, authors, statistics
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/dash-author-leaderboard/

## Code

```php
add_action( 'wp_dashboard_setup', function () {
    wp_add_dashboard_widget( 'wpide_author_leaderboard', 'Author leaderboard', function () {
        global $wpdb;

        $data = get_transient( 'wpide_author_leaderboard' );
        if ( false === $data ) {
            $this_month = wp_date( 'Y-m-01 00:00:00' );
            $last_month = wp_date( 'Y-m-01 00:00:00', strtotime( '-1 month', strtotime( $this_month ) ) );
            $data       = $wpdb->get_results( $wpdb->prepare(
                "SELECT post_author, SUM( post_date >= %s ) AS current_month, SUM( post_date < %s ) AS previous_month
                FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' AND post_date >= %s
                GROUP BY post_author ORDER BY current_month DESC, previous_month DESC LIMIT 10",
                $this_month,
                $this_month,
                $last_month
            ) );
            set_transient( 'wpide_author_leaderboard', $data, HOUR_IN_SECONDS );
        }

        if ( ! $data ) {
            echo '<p>No posts published since last month.</p>';
            return;
        }

        echo '<table class="widefat striped"><thead><tr><th>Author</th><th>This month</th><th>Last month</th></tr></thead><tbody>';
        foreach ( $data as $row ) {
            printf(
                '<tr><td>%s %s</td><td><strong>%d</strong></td><td>%d</td></tr>',
                get_avatar( $row->post_author, 20, '', '', array( 'extra_attr' => 'style="vertical-align:middle;border-radius:50%"' ) ),
                esc_html( get_the_author_meta( 'display_name', $row->post_author ) ),
                (int) $row->current_month,
                (int) $row->previous_month
            );
        }
        echo '</tbody></table>';
    } );
} );
```

## Notes

Counts published posts only and are cached for an hour.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Author Leaderboard Widget" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
