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);
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: