I have been building this recent-post-count dashboard widget graph - or, more accurately, modifying and refining a comment-specific version, for posts.

I'm really pleased. Some of it may be inefficient, I'm learning as I go along, but I got it working...

enter image description here

You see, for each date, starting at the specific date, it increments through each date up to the current one, generating a WP_Query to get the post count for that date, adding each to an array, which powers the chart.

 * Plugin Name: Dashboard Widget for Comments
 * Description: Displays a comment-count graph in a dashboard widget. Code inspired by WPMUDev article https://premium.wpmudev.org/blog/adding-custom-widgets-to-the-wordpress-admin-dashboard/
 * Version: 1.0.0
 * Author: Robert Andrews

 function dashboard_widget_display_enqueues( $hook ) {
    if( 'index.php' != $hook ) {

    wp_enqueue_style( 'dashboard-widget-styles', plugins_url( '', __FILE__ ) . '/widgets.css' );

 add_action( 'admin_enqueue_scripts', 'dashboard_widget_display_enqueues' );

 function register_comment_stats_dashboard_widget() {
        'Post Stats',

 add_action( 'wp_dashboard_setup', 'register_comment_stats_dashboard_widget' );

 function comment_stats_dashboard_widget_display() {

   // Initialise array to hold our daily post counts
   $comment_counts = array();

   // Number of days to chart
   $num_days = 14;
   // Start at this time ago
    $date = new DateTime('-'.$num_days.' days');

   // For each day of the period
    for (
     $x = 1; // Initial value
     $x <= $num_days; // Iterate this many times
     $x++ // Increment each time
   ) {
     // Go to next day
     $date->modify('+1 day');
     // echo 'Date: '.$date->format('Y-m-d') . '<br />';
     $day_before = new DateTime($date->format('Y-m-d').' -1 day');
     // echo 'day_before: '.$day_before->format('Y-m-d') . '<br />';
     $day_after = new DateTime($date->format('Y-m-d').' +1 day');
     // echo 'day_after: '.$day_after->format('Y-m-d') .'<br />';

     // WordPress query for posts in time period
     $date_query = array(
                             'inclusive' => false,
     $args = array(
                     'post_type' => 'quote',
                     // 'category__in' => (array)$ids,
                     'date_query' => $date_query,
                     'no_found_rows' => true,
                     'suppress_filters' => true,
                     'orderby' => 'ID'
     $query = new WP_Query( $args );

     // echo $query->post_count;

     // Add daily post count to the array
     $comment_counts[] = $query->post_count;


    // eg. $comment_counts = array( 20, 29, 39, 33, 17, 12, 2, 20, 29, 39, 33, 17, 12, 2 );
    $highest_value = max( $comment_counts );
    $data_points = count( $comment_counts );

    $bar_width = 100 / $data_points - 2;

    $total_height = 120;


    <div class="comment-stat-bars" style="height:<?php echo $total_height ?>px;">
            foreach( $comment_counts as $count ) :
                $count_percentage = $count/$highest_value;
                $bar_height = $total_height * $count_percentage;
                $border_width = $total_height - $bar_height;
        <div class="comment-stat-bar" style="height:<?php echo $total_height ?>px; border-top-width:<?php echo $border_width ?>px; width: <?php echo $bar_width ?>%;"></div>
        <?php endforeach ?>

    <div class='comment-stat-labels'>
        <?php foreach( $comment_counts as $count ) : ?>
        <div class='comment-stat-label' style='width: <?php echo $bar_width ?>%;'><?php echo $count ?></div>
    <?php endforeach ?>

    <div class='comment-stat-caption'>Posts added in the last <?php echo $num_days; ?> days</div>



Now I'd like to add two things, below the post count on the chart...

  1. the day number (eg. "10") and
  2. the initial of the day (eg. "S" for "Saturday")

Here is what I am guessing...


I guess this may mean populating the $comment_count array with not just values but keys and values, wherein the key is the related date, $date->format('Y-m-d'). I don't know anything about this type of array. Is this an "associative" array? How would I modify...

 // Add daily post count to the array
 $comment_counts[] = $query->post_count;

... to do that?


Then maybe, in the foreach( $comment_counts as $count ) that steps through the $comment_count array, I would need to turn the stored key date in to both a "10" and an "S"? How would I do that?

Am I on the right track?


Here is the finished product...

enter image description here

  • What was the purpose of $hook in this? I modified to use on front end also so I added wp_enqueue_style for that, but the if statement stopped it working so I removed it. Thanks for posting this question as I'm using it now for displaying summary of service calls to a helpdesk which are logged in a CPT. – TomC Feb 22 '20 at 8:23
  • @TomC I suggest checking the original article, linked in the question, on which my script was based. Thanks. – Robert Andrews Feb 22 '20 at 8:29

I would go like this:

A. Use the Unix timestamp as array key.

$comment_counts[$date->format('U')] = $query->post_count;

B. Loop with key included.

foreach( $comment_counts as $count_key => $count ) :

Then you obtain the day number and the initial of the day from the key:

// "d" means with leading zero, use "j" in place of "d" for no leading zero 
$day_number = date("d", $count_key);
$day_initial = substr(date("D" , $count_key), 0, 1);
  • Does this foreach loop mean replacing or adding to my current for ($x = 1... loop? – Robert Andrews Mar 11 '18 at 15:19
  • No, that can be left intact. If I am not wrong, you should only change the other two existing foreach loops as shown in point B and within these loops (or in another identical loop) you can extract the day number and initial from the key. – Stefano Tombolini Mar 11 '18 at 17:13
  • This is phenomenal. I was going to write to say I could only get to show the numbers/initials all compressed in to the final column, but having another go at positining the code worked lovely. Thanks. – Robert Andrews Mar 11 '18 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.