public
Created

  • Download Gist
chart.js
JavaScript
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
function ch_count_months_js_output($args = ''){
global $wpdb, $wp_locale;
$defaults = array(
'limit' => '',
'before' => ''
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
$where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
$join = apply_filters('getarchives_join', "", $r);
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
$key = md5($query);
$cache = wp_cache_get( 'ch_count_months_js_output' , 'general');
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_add( 'ch_count_months_js_output', $cache, 'general' );
} else {
$arcresults = $cache[ $key ];
}
if ( $arcresults ) {
$arcresults = array_reverse($arcresults);
$posts = array();
$months = array();
foreach ( (array) $arcresults as $arcresult ) {
$month = sprintf(__('%1$s'), $wp_locale->get_month($arcresult->month));
array_push($months, $month);
array_push($posts, $arcresult->posts);
}
}
print_r($months);
print_r($posts);
echo '<p type="text/javascript">';
echo 'var months = ["', join($months,'","'), '"];';
echo 'var posts = [', join($posts,','), ']';
echo '</p>';
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.