Halloween Sale 15% off Ends in See the plans
PHP

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.

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

Code

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 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.