views:

26

answers:

2

Hello guys,

I was hoping to get some ideas as how I can get a number of posts that have been posted on our blog BUT within a certain date range. I know how to get total number but need this extra functionality.

This is what I have for getting total number of posts:

$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts);

Then I just echo out where ever I want to display that number

<?php if ( is_page('wordpress-numbers')) {
echo "<strong>".$numposts.' posts have been published since August 12, 2009'."</strong>";
}
?>

For example I want to know the number of posts in the last 7 days. Maybe use a datepicker of sorts?

Thanks,

Matt

+3  A: 

Sure, you could do it by setting a date range in a custom query:

$today = date("Y-m-d");
$sevenDaysAgo = date("Y-m-d", mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));

$querystr = "
    SELECT wposts.*
    FROM $wpdb->posts wposts,
    WHERE wposts.post_date BETWEEN '" . $today . "' AND '" . $sevenDaysAgo . '" 
    AND wposts.post_type = 'post'
    ORDER BY wposts.post_date DESC
";
Pat
Thanks Pat... but this also gives me something close to what I want... question is how do I just get a number of posts? $week = date('W'); $year = date('Y'); query_posts('year=' . $year .'
Matthew
+1  A: 

Okay thanks to Pat he got me thinking here and I came up with this:

$querystr = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_date >= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY) LIMIT 0, 30");
if (0 < $querystr) $querystr = number_format($querystr);

Then I will just echo out the result:

echo "<strong>".$querystr.' posts have been published in the last 7 days'."</strong>";

The next thing I would like to do is add the ability to choose the start date then show results for those 7days.

Thanks again Pat

Matthew
No worries - glad you got it sorted out. A calendar widget would be a good way to do it. You could even get fancy all allow the user to pick the start and end date of the range.
Pat
@pat... yeah I was thinking about doing that next.
Matthew