Last active
January 7, 2017 15:43
-
-
Save subhashdasyam/b8efaafc7fe80c0ec048f5c971ec1563 to your computer and use it in GitHub Desktop.
Wordpress Get Per Day posts for past 30 days
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 | |
//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