Blog / WordPress/ WordPress: Get Post Count for This Week, Today, and Last 24 Hours (Code Implementation)

WordPress: Get Post Count for This Week, Today, and Last 24 Hours (Code Implementation)

WordPress 获取本周、今日及24小时内发布的文章数量(代码实现)

In WordPress development, you may need to count posts published within specific timeframes, such as for displaying site activity or generating reports. This article provides reliable methods to get post counts for this week, today, and the last 24 hours.

Method 1: Get This Week's Post Count

This method uses WP_Query with a date_query parameter to query posts from the past week. It's efficient and follows WordPress best practices.

function get_week_post_count() {
    $args = array(
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'date_query'     => array(
            array(
                'after' => '1 week ago'
            )
        ),
        'no_found_rows'  => true,
        'fields'         => 'ids',
        'posts_per_page' => -1,
    );
    $query = new WP_Query( $args );
    return $query->post_count;
}

Usage

Add the code to your theme's functions.php. Call it in your template:

<?php echo get_week_post_count(); ?>

Method 2: Get Today's Post Count

This method uses $wpdb to query the database directly, comparing dates to get posts published today. Note: it depends on your server's timezone settings.

function get_posts_count_from_today( $post_type = 'post' ) {
    global $wpdb;
    $today = current_time( 'Y-m-d' );
    $numposts = $wpdb->get_var(
        $wpdb->prepare(
            "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = %s AND DATE(post_date) = %s",
            $post_type,
            $today
        )
    );
    return (int) $numposts;
}

Usage

Add to functions.php. Call it:

<?php echo get_posts_count_from_today(); ?>

For other post types (e.g., 'product'):

<?php echo get_posts_count_from_today( 'product' ); ?>

Method 3: Get Last 24 Hours Post Count

This method also uses $wpdb to query posts published in the last 24 hours from the current time.

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

Usage

Add to functions.php. Call it:

<?php echo get_posts_count_from_last_24h(); ?>

For other post types:

<?php echo get_posts_count_from_last_24h( 'product' ); ?>

Important Notes & Best Practices

  1. Performance: Method 1 uses WP_Query with no_found_rows and fields => 'ids' for better performance. Methods 2 and 3 query the database directly, which can be faster with large datasets, but always use prepare() to prevent SQL injection.
  2. Timezone: For 'today' and '24 hour' queries, always use current_time() instead of PHP's date() or time() to match WordPress's timezone setting.
  3. Caching: If counts don't need to be real-time, consider caching results with the Transients API to improve page load speed.
  4. Error Handling: In production, validate function returns (e.g., with intval()) to ensure integer output.

These code snippets are ready to use. Choose the method that fits your needs and integrate it into your theme or plugin.

Post a Comment

Your email will not be published. Required fields are marked with *.