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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.