0

how can I display the number of posts published daily/weekly ? I don't care about caching, i can even disable it. Obviously if there is a cached way it would be more helpful.

0

Since the publish date is stored, you can simply range-select count for posts, which are published in a given interval range.

function count_posts_per_interval($seconds) {
    global $wpdb;

    $count = (array) $wpdb->get_row($wpdb->prepare(
        "select count(ID) from wp_posts where post_status = 'publish' and post_date between date_sub(now(), interval %d second) and now();",
        $seconds
    ));

    return (int) array_shift($count);
}

The SQL query can take day,week etc instead of second, but since WordPress ships with some useful time constants, let's keep it that way.

# Retrieve number of posts posted within last day
print_r( count_posts_per_interval( DAY_IN_SECONDS ) );

# Retrieve number of posts posted within last week
print_r( count_posts_per_interval( WEEK_IN_SECONDS ) );
  • I added the function in theme-->function.php and the SQL query in a html file with <?php ?> tag but seems not working – Roberto Scardigno Oct 14 '17 at 11:31
  • Could you help me ? – Roberto Scardigno Oct 16 '17 at 9:19
  • Sorry. PHP is not executed in .html files. – Samuel Elh Oct 16 '17 at 12:27
  • Sorry for the wasting of time :D. How could I let it work then ? – Roberto Scardigno Oct 17 '17 at 13:11
  • No worries Roberto, you can tell me exactly what you want (like, where you'd like to place this on the site) and I'll instruct you with the right code to plug this into your site. – Samuel Elh Oct 17 '17 at 23:45

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.