2

I'm learning about caching parameters and tried this in the single.php template:

echo count($wp_object_cache->cache['posts']);

$query = new \WP_Query([
  'post_type' => 'post',
  'cache_results' => false,
  'posts_per_page' => 5,
  'ignore_sticky_posts' => true
]);

echo '<br>' . count($wp_object_cache->cache['posts']);

The result:

3
7

Somehow, the cache_results parameter didn't work. Am I missing something?


I'm using:

  • WordPress version 4.6.
  • Twentyfifteen theme.
  • No plugins/third party services.
2

The posts array result in WP_Query is mapped to get_post() (here and here) with:

$this->posts = array_map( 'get_post', $this->posts );

and that seems to be adding posts to the object cache even though the cache_results argument is set to false in WP_Query.

Within the get_post() function (here) we have:

$_post = WP_Post::get_instance( $post );

for the case when $post is neither a WP_Post or an object instance.

The WP_Post::get_instance method contains wp_cache_get() and wp_cache_set() calls.

So that could explain the behavior you see in your example.

  • Ah, thank you! I understand the problem now. But, has WordPress intended to do that or should we submit a ticket to WordPress Trac? – MinhTri Aug 22 '16 at 9:10
  • It sounds like a bug to me, so you should submit a ticket. If it's closed as an expected behavior, then at least we get the explanation for it ;-) @Dan9 – birgire Aug 22 '16 at 9:12
  • You meant wp_cache_add() or wp_cache_set()? Btw, I have never submitted a ticket before but i will give a try. :-) – MinhTri Aug 22 '16 at 9:20
  • The get_instance() method contains both a wp_cache_get() and a wp_cache_set() call. It's easy to create a ticket, but you should first check if a similar ticket has been created before, to avoid it being closed as a duplicate. Good luck and please keep us informed ;-) @Dan9 – birgire Aug 22 '16 at 9:25
  • 1
    Done! Thank you! Hope we can get responses soon! Here is the link to the ticket. – MinhTri Aug 22 '16 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.