Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

Code

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 add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.