Skip to content

Instantly share code, notes, and snippets.

@m
Created December 15, 2017 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m/2f99d954564a31f1cff3a3cf9672f76b to your computer and use it in GitHub Desktop.
Save m/2f99d954564a31f1cff3a3cf9672f76b to your computer and use it in GitHub Desktop.
<table cellspacing="10">
<tr>
<th width="50"></th>
<th>Posts</th>
<th>Avg. Words</th>
<th>Total Words</th>
<th>Avg. Comments</th>
<th>Total Comments</th>
</tr>
<?php
$distinct_years = $wpdb->get_col( "SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE YEAR(post_date) != 0 ORDER BY YEAR(post_date) ASC" );
foreach( $distinct_years as $year ) {
$total_posts = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE YEAR(post_date) = '$year' AND post_status = 'publish'" );
$total_words = 0;
$all_content = $wpdb->get_col( "SELECT post_content FROM $wpdb->posts WHERE YEAR(post_date) = '$year' AND post_status = 'publish'" );
foreach ( $all_content as $c ) {
$c = strip_tags( $c );
$c = preg_replace( "/\s+/", ' ', $c );
$words = explode( ' ', $c );
$total_words += count( $words );
}
$avg_words = floor( $total_words / $total_posts );
$total_comments = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments WHERE YEAR(comment_date) = '$year' AND comment_approved = '1'" );
$avg_comments = floor( $total_comments / $total_posts );
$total_posts = number_format( $total_posts );
$avg_words = number_format( $avg_words );
$total_words = number_format( $total_words );
$avg_comments = number_format( $avg_comments );
$total_comments = number_format( $total_comments );
echo "<tr><td>$year</td><td align='right'>$total_posts</td><td align='right'>$avg_words</td><td align='right'>$total_words</td><td align='right'>$avg_comments</td><td align='right'>$total_comments</td></tr>";
}
?>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment