Reuse WordPress Cache on single post template

I’m on a single custom page template.


if i var_dump $post or $posts i will retrieve only one post

$post will be an object(WP_Post)
$posts will be an array (size=1) 0 => object(WP_Post)

At the end of the page i need to show a list of posts from this same custom-posts

So instead of doing a WP_Query i looked into the php globals to see if by any chance wp has a list of the posts that i need in cache.

And i found this object object(WP_Object_Cache)

So i looked into the codex and found this function wp_cache_get()

And looking on this function code does this.

function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
    global $wp_object_cache;

    return $wp_object_cache->get( $key, $group, $force, $found );

on the $wp_object_cache i found this

  private 'cache' => 
    array (size=6)
      'options' => 
        array (size=2)
          'alloptions' => 
            array (size=116)
       'posts' => 
            array (size=17)

On the posts node has everything that i need, but i’m trying to use wp_cache_get('posts') and it return false.

If WP already have this information stored on cache i don’t need to query again and save time and resources

Is there a right way to use this or i need to:

global $wp_object_cache;
$postsToLoop = $wp_object_cache->cache['posts'];

Read more here: Reuse WordPress Cache on single post template

Leave a Reply

Your email address will not be published. Required fields are marked *