Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Someone gave me this SQL code to count the number of posts (of post_type 'mixtapes') in the last 7 days. I tried to convert it to 'in the last 24 hours' as well, but it's giving the wrong number.

Here's his code which works for 7 day period:

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

Here's my code for 1 day period which gives wrong answer

$querystr = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'mixtapes' AND post_date >= DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY) LIMIT 0, 30");
if (0 < $querystr) $querystr = number_format($querystr);
share|improve this question
    
What result are you getting vs what you think you should get? I ran that query against some of my tables and came up with what I expected. For simplicity sake you can remove the "LIMIT 0, 30" from the end of your query, as you'll only have one number as a result. –  Sandbox Wizard Apr 20 '12 at 2:58
add comment

1 Answer

up vote 0 down vote accepted

Personally, I'd generate the date in php using the PHP Date class and then $date->sub(). With a few more lines of code the query will read:

... WHERE post_date >= ".$date." ...
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.