wp_cache_get

80

function prefix_get_post_count( $post_status = 'publish' ) {
    $cache_key = 'prefix_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;
}

Comments

Submit
0 Comments