Last active
September 24, 2016 21:57
-
-
Save pj-dave/5611108114bd4ebc39d3b1eaf31fec5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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