Skip to content

Instantly share code, notes, and snippets.

@waqashassan98
Created November 20, 2019 11:51
Show Gist options
  • Save waqashassan98/8324477e53e3472cb4ce3544cc0e0f7f to your computer and use it in GitHub Desktop.
Save waqashassan98/8324477e53e3472cb4ce3544cc0e0f7f to your computer and use it in GitHub Desktop.
Add and retrieve objects from wp cache
<?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