# Posts per Month Chart Widget

> Adds a dashboard widget with a simple bar chart of how many posts were published in each of the last 12 months, to keep an eye on your publishing rhythm.

- Type: PHP
- Runs: Admin only
- Category: [Dashboard](https://wpide.com/library/category/dashboard/)
- Tags: dashboard-widget, chart, publishing
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/dash-posts-per-month-chart/

## Code

```php
add_action( 'wp_dashboard_setup', function () {
    wp_add_dashboard_widget( 'wpide_posts_per_month', 'Posts per month', function () {
        global $wpdb;

        $rows = get_transient( 'wpide_posts_per_month' );
        if ( false === $rows ) {
            $rows = $wpdb->get_results( $wpdb->prepare(
                "SELECT DATE_FORMAT(post_date, '%%Y-%%m') AS ym, COUNT(*) AS total FROM {$wpdb->posts}
                WHERE post_type = 'post' AND post_status = 'publish' AND post_date >= %s GROUP BY ym",
                wp_date( 'Y-m-01 00:00:00', strtotime( '-11 months', current_time( 'timestamp' ) ) )
            ), OBJECT_K );
            set_transient( 'wpide_posts_per_month', $rows, 6 * HOUR_IN_SECONDS );
        }

        $months = array();
        for ( $i = 11; $i >= 0; $i-- ) {
            $stamp = strtotime( "-$i months", strtotime( wp_date( 'Y-m-01' ) ) );
            $key   = gmdate( 'Y-m', $stamp );
            $months[ $key ] = array( wp_date( 'M', $stamp ), isset( $rows[ $key ] ) ? (int) $rows[ $key ]->total : 0 );
        }
        $max = max( 1, max( array_column( $months, 1 ) ) );

        echo '<div style="display:flex;align-items:flex-end;gap:4px;height:140px;padding-top:16px" role="img" aria-label="Posts published per month">';
        foreach ( $months as $month ) {
            printf(
                '<div style="flex:1;text-align:center;font-size:11px;color:#646970"><div style="font-weight:600;color:#1d2327">%2$d</div><div style="height:%1$dpx;background:#2271b1;border-radius:3px 3px 0 0;margin:2px 0"></div>%3$s</div>',
                (int) round( $month[1] / $max * 100 ),
                (int) $month[1],
                esc_html( $month[0] )
            );
        }
        echo '</div>';
    } );
} );

add_action( 'publish_post', function () {
    delete_transient( 'wpide_posts_per_month' );
} );
```

## Notes

Pure HTML and CSS, no chart library; counts are cached for six hours and refreshed when a post is published.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Posts per Month Chart 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/
