Created
January 7, 2017 20:35
-
-
Save subhashdasyam/776335f22650a9e147a1b55a3907585a to your computer and use it in GitHub Desktop.
Wordpress Post Stats with Google Visualisation
This file contains hidden or 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
<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> |
This file contains hidden or 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 | |
//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