Skip to content

Instantly share code, notes, and snippets.

@subhashdasyam
Last active January 7, 2017 15:43
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/b8efaafc7fe80c0ec048f5c971ec1563 to your computer and use it in GitHub Desktop.
Save subhashdasyam/b8efaafc7fe80c0ec048f5c971ec1563 to your computer and use it in GitHub Desktop.
Wordpress Get Per Day posts for past 30 days
<?php
//Last 30 days dates
$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')));
}
//Vars
$each_day_count = array();
$i = 1;
foreach($dates as $date){
//$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' => 'ASC',
'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
$i += 1;
}
print_r($each_day_count);
// Output should be
//Example below
// array(1=>20,2=>1,....);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment