Skip to content

Instantly share code, notes, and snippets.

@subhashdasyam
Created January 7, 2017 20:35
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 subhashdasyam/776335f22650a9e147a1b55a3907585a to your computer and use it in GitHub Desktop.
Save subhashdasyam/776335f22650a9e147a1b55a3907585a to your computer and use it in GitHub Desktop.
Wordpress Post Stats with Google Visualisation
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.charts.load('current', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "wordpress_post_stats_google_visualization.php",
dataType: "json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240});
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
<?php
//Vars
$i = 1;
//Past 30 days
$dates = array();
for($i = 0; $i < 30; $i++) {
$dates[] = array('day'=> date("d", strtotime('-'. $i .' days')), 'month'=>date("m", strtotime('-'. $i .' days')), 'year'=>date("Y", strtotime('-'. $i .' days')));
}
$table = array();
$table['cols'] = array(
/* define your DataTable columns here
* each column gets its own array
* syntax of the arrays is:
* label => column label
* type => data type of column (string, number, date, datetime, boolean)
*/
array('label' => 'Day', 'type' => 'number'),
array('label' => 'Posts', 'type' => 'number')
);
$rows = array();
foreach($dates as $date){
$temp = array();
//$args = array('year' => $today["year"], 'monthnum' => $today["mon"], 'day' => $today["mday"], 'nopaging' => true, 'post_status' => 'publish');
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'nopaging' => true,
// Using the date_query to filter posts from everyday
'date_query' => array(
array(
'year' => $date['year'],
'month' => $date['month'],
'day' => $date['day'],
),
)
);
$perdayposts = new WP_Query($args);
//$each_day_count[$i] = $perdayposts->found_posts; //count without pagination
$temp[] = array('v' => $i);
$temp[] = array('v' => (int) $perdayposts->found_posts);
$rows[] = array('c' => $temp);
$i += 1;
}
// populate the table with rows of data
$table['rows'] = $rows;
// encode the table as JSON
$jsonTable = json_encode($table);
// set up header; first two prevent IE from caching queries
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
// return the JSON data
echo $jsonTable;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment