djekl
1/3/2012 - 4:28 AM

WordPress per-year blog stats

WordPress per-year blog stats

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