public
Last active

WordPress per-year blog stats

  • Download Gist
wp-stats.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
<?php
 
header('Content-type: text/plain');
global $wpdb;
 
?>
<table class="numbers">
<thead>
<tr>
<th>&nbsp;</th>
<th>Posts</th>
<th>Avg. Length</th>
<th>Total Length</th>
<th>Comments (Mine)</th>
</tr>
</thead>
<tbody>
<?php
 
for ($i = intval(date('Y')); $i >= 2002; $i--) {
 
$from = $i;
$to = $i + 1;
 
$posts_count = $wpdb->get_var("
SELECT COUNT(*)
FROM `wp_posts`
WHERE post_date >= '$from-01-01'
AND post_date < '$to-01-01'
AND post_status = 'publish';
");
$posts_length_avg = $wpdb->get_var("
SELECT AVG(LENGTH(post_content))
FROM `wp_posts`
WHERE post_date >= '$from-01-01'
AND post_date < '$to-01-01'
AND post_status = 'publish';
");
$posts_length_total = $wpdb->get_var("
SELECT SUM(LENGTH(post_content))
FROM `wp_posts`
WHERE post_date >= '$from-01-01'
AND post_date < '$to-01-01'
AND post_status = 'publish';
");
$comments_total = $wpdb->get_var("
SELECT COUNT(*)
FROM `wp_comments`
WHERE comment_date >= '$from-01-01'
AND comment_date < '$to-01-01'
AND comment_approved = '1';
");
$comments_mine = $wpdb->get_var("
SELECT COUNT(*)
FROM `wp_comments`
WHERE comment_date >= '$from-01-01'
AND comment_date < '$to-01-01'
AND comment_approved = '1'
AND user_ID = 1;
");
 
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo number_format($posts_count, 0); ?></td>
<td><?php echo number_format($posts_length_avg, 0); ?></td>
<td><?php echo number_format($posts_length_total, 0); ?></td>
<td><?php echo number_format($comments_total, 0); ?> (<?php echo number_format($comments_mine, 0); ?>)</td>
</tr>
<?php
}
?>
</tbody>
</table>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.