Skip to content

Instantly share code, notes, and snippets.

@pj-dave
Last active September 24, 2016 21:57
Show Gist options
  • Save pj-dave/5611108114bd4ebc39d3b1eaf31fec5e to your computer and use it in GitHub Desktop.
Save pj-dave/5611108114bd4ebc39d3b1eaf31fec5e to your computer and use it in GitHub Desktop.
<?php
add_action( 'init', function() {
global $wpdb;
$years = 5;
$batch = 500;
if ( empty( $_GET['pageviews-jetpack-stats'] ) )
return;
$config = get_option( 'pageviews_config' );
echo $config['account'] . PHP_EOL;
$post_types = array_diff( get_post_types( array( 'public' => true ), 'names' ), array( 'attachment' ) );
$post_types = implode( ',', array_map( function( $i ) { return "'{$i}'"; }, $post_types ) );
$ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type IN ({$post_types}) AND post_status = 'publish' ORDER BY ID ASC;" );
foreach ( array_chunk( $ids, $batch ) as $chunk ) {
$data = stats_get_csv( 'postviews', array(
'summarize' => true,
'days' => 365 * $years,
'limit' => 500,
'post_id' => implode( ',', $chunk ),
) );
foreach ( $data as $item ) {
printf( "%d:%d\n", $item['post_id'], $item['views'] );
}
}
die();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment