I am wondering what is the best way to cache a Wp_Query that can have from 10 items to 10000. I know it's a bad practice but for specific reason I have to process all this post types all together and depends on the installation the volume of the items.

Looking on https://codex.wordpress.org/Class_Reference/WP_Object_Cache seems that I can cache pretty easily but I wondering if save the output of Wp_Query (after parsing) using this method is a good solution because for this edge case.

Actually I cache this parsing in the class in a variable to avoid multiple execution, but keep this cache between different calls to the website will be not a bad idea.

  • WP_Object_Cache won't help because it only stores data for the life of the request. It sounds like you want a Transient. Mar 1, 2018 at 23:49
  • @JacobPeattie Yes and no. WP_Object_Cache can be used for persistent object caching with wp-content/object-cache.php drop-in and an object cache backend (memcached/redis/etc). Mar 2, 2018 at 0:37
  • Well yeah, if you add functionality that does that sure, but on its own WP_Object_Cache is not persistent. Mar 2, 2018 at 1:28
  • wp object cache is not persistent by default, wp transients api is. This may be what you are looking for.
    – Joel M
    Mar 2, 2018 at 2:03
  • You really shouldn‘t store the whole query in the cache.. Store either the resulting post IDs or post objects.
    – swissspidy
    Mar 2, 2018 at 9:16


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.