Created
November 20, 2019 11:51
-
-
Save waqashassan98/8324477e53e3472cb4ce3544cc0e0f7f to your computer and use it in GitHub Desktop.
Add and retrieve objects from wp cache
This file contains hidden or 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 | |
function get_post_count( $post_status = 'publish' ) { | |
$cache_key = 'post_count_'. $post_status; | |
$_posts = wp_cache_get( $cache_key ); | |
if ( false === $_posts ) { | |
$_posts = $wpdb->get_var( | |
$wpdb->prepare( | |
"SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = %s", | |
$post_status | |
)); | |
wp_cache_set( $cache_key, $_posts ); | |
} | |
return $_posts; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment