0

I need to display the number of posts that were added TODAY

I found this one that will show number of all posts but I need only last day

<?php
$total = wp_count_posts()->publish;
echo 'Total Posts: ' . $total;
?> 

I've a felling that it's very easy but I coudn't any script that will work :(

Thank You in advance, Peter

2

You can use WP_Query like this:

// we get the date for today
$today = getdate();
//we set the variables, i am ignoring sticky posts so they dont get counted
$args = array(
    'ignore_sticky_posts' => 1,
    'posts_per_page' => -1, //all posts 
    'date_query' => array(
        array(
            'year'  => $today["year"],
            'month' => $today["mon"],
            'day'   => $today["mday"],
        ),
    ),
);

//we create the query
$today_posts = new WP_Query( $args );
//the result already has a property with the number of posts returned
$count = $today_posts->post_count;
//show it
echo $count;
| improve this answer | |
  • kk it wasn't that easy hehe Thank You it's working great:) – user262404 Mar 15 '17 at 19:16
  • i updated the answer, i added 'posts_per_page' => -1 to display all posts, it can be done with 'nopaging' => true too – David Lee Mar 15 '17 at 21:04
  • I figured it by myself using nopaging but Thanks again for your help – user262404 Mar 16 '17 at 14:59
  • Script work perfect! Any idea how to add +3h from GMT as this work accordign GMT. Thanks – Nick Apr 29 at 21:55
  • @Nick I didnt test this but you need figurete out the GMT value first then added a new array like this array( 'hour' => 03, 'compare' => '>=', ), ) where the 3 is the value you found – David Lee Apr 30 at 4:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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