Show weekly posts statistic in WordPress

You already have the total post, so in order to get the total post within the last week, it’s better if you do this on your functions.php file:

function get_posts_count_from_last_week($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('-168 hours'))
        )
    );
    return $numposts;
}

And then use it in the footer.

<?php echo get_posts_count_from_last_week(); ?>

To work with categories we could use WP_Query:

$args = array(
  'cat' => 4,
  'post_type' => 'videos'
);
$the_query = new WP_Query( $args );
echo $the_query->found_posts;

Leave a Comment