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.

I would like to display count for custom post types (or posts) which was published that day (last 24 hours or TODAY). I use this snippet to get count of all posts from "posts" or from "custom post types"

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

in templates:

<?php echo $numposts ?>

but I don't know how to display count only for last 24 hours or TODAY (it means from midnight to midnight)

share|improve this question
    
Have you tried our search? It brings up some useful solutions, this one for example. –  toscho Jul 13 '13 at 13:24
    
thanks for your link @toscho, I use a search function a lot and this one I haven't found, I have found another one 10 minutes ago which will probably help me to achieve the same with my code above link is here –  Daniel Foltynek Jul 13 '13 at 14:06

1 Answer 1

up vote 3 down vote accepted

You can write your own function (add it to functions.php file:

function get_posts_count_from_last_24h($post_type ='post') {
    global $wpdb;

    $numposts = $wpdb->get_var(
        $wpdb->prepare(
            "SELECT COUNT(ID) ".
            "FROM {$wpdb->posts} ".
            "WHERE ".
                "post_status='publish' ".
                "AND post_type= %s ".
                "AND post_date> %s",
            $post_type, date('Y-m-d H:i:s', strtotime('-24 hours'))
        )
    );
    return $numposts;
}

function get_posts_count_from_today($post_type ='post') {
    global $wpdb;

    $numposts = $wpdb->get_var(
        $wpdb->prepare(
            "SELECT COUNT(ID) ".
            "FROM {$wpdb->posts} ".
            "WHERE post_status='publish' ".
                "AND post_type= %s ".
                "AND DATE_FORMAT(post_date, '%Y-%m-%d') = %s",
            $post_type, date('Y-m-d', time())
        )
    );
    return $numposts;
}

And then use it in template:

<?php echo get_posts_count_from_last_24h(); ?>
<?php echo get_posts_count_from_today(); ?>
share|improve this answer
    
Thank you very much Krzysiek, I need to use it for 4 custom post types in one place, this should serve as a daily stats of new posts without remembering how many posts were published until yesterday, it should tell me for all post_types if there are some new posts or not, can we have the code in functions.php more general for all custom post types including posts (without repeating the same code) and template tags specific for every post_type in order to use them together? –  Daniel Foltynek Jul 13 '13 at 13:59
    
Two notes: That could be a Transient and it should not go into functions.php. It's functionality and belongs into a plugin. –  kaiser Jul 13 '13 at 14:12
    
@DanielFoltynek: Of course. Just modify SQL queries inside these functions. You can of course ad them as params (so you don't have to copy these functions for each of custom post types). –  Krzysiek Dróżdż Jul 13 '13 at 14:25
    
@kaiser: If you will use it in your theme, then you can add it as theme function. There is NO NEED to make a special plugin just to add 2 functions... Especially when other themes don't have to use these functions. –  Krzysiek Dróżdż Jul 13 '13 at 14:26
1  
Why wouldn't I use it? I got plenty of per-project plugins. Some hundreds even. Much easier to debug, maintain, etc. Only difference is that I always add the output to hooks or filters instead of plain function calls, so it doesn't throw errors if the plugin is deactivated and the function not present. Also this would be something I could reuse in later projects. –  kaiser Jul 13 '13 at 14:45

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.