Created
August 24, 2014 09:32
-
-
Save ahmadawais/68f52fdfdd959492f514 to your computer and use it in GitHub Desktop.
Fetch Number of posts in Last 24 hours for WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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