Skip to content

Instantly share code, notes, and snippets.

@ahmadawais
Created August 24, 2014 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadawais/68f52fdfdd959492f514 to your computer and use it in GitHub Desktop.
Save ahmadawais/68f52fdfdd959492f514 to your computer and use it in GitHub Desktop.
Fetch Number of posts in Last 24 hours for WordPress
<?php /* Template Name: Test AA */
get_header(); ?>
<!-- section -->
<section class="aa_sec1">
<?php echo aa_get_posts_count_from_last_24h(); ?>
<?php echo aa_get_posts_count_from_today(); ?>
</section>
<!-- /section -->
<?php //get_sidebar(); ?>
<?php get_footer(); ?>
// Load any external files you have here
function aa_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 aa_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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment