{"id":20174,"date":"2019-06-14T00:20:27","date_gmt":"2019-06-14T00:20:27","guid":{"rendered":"https:\/\/kb.pressable.com\/?post_type=article&#038;p=12326"},"modified":"2025-09-11T14:45:34","modified_gmt":"2025-09-11T19:45:34","slug":"using-wordpress-object-cache-for-query-results","status":"publish","type":"knowledgebase","link":"https:\/\/pressable.com\/knowledgebase\/using-wordpress-object-cache-for-query-results\/","title":{"rendered":"Using the WordPress Object Cache to Cache Query Results"},"content":{"rendered":"\n<p><span style=\"font-weight: 400;\">If you have a query that is taking a long time to load, more often than not that query can be cached for a limited amount of time with WordPress object cache. <\/span>As a result, your site will load faster (i.e. TTFB will be reduced) and function more efficiently.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"identify-the-slow-query\" data-offset=\"125\" class=\"content-heading toc-content__target\">Identify the slow query<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/pressable.com\/wp-content\/uploads\/2019\/06\/querytime.png?ssl=1\" alt=\"Identifying a slow query using the Debug bar from Pressable\" class=\"wp-image-12329\"\/><\/figure>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Plugins like <a href=\"https:\/\/wordpress.org\/plugins\/query-monitor\/\" target=\"_blank\" rel=\"noreferrer noopener\">Query Monitor<\/a> and the <a href=\"https:\/\/wordpress.org\/plugins\/debug-bar\/\" target=\"_blank\" rel=\"noreferrer noopener\">Debug Bar<\/a> can help you identify slow queries on your site and which PHP functions are executing them. The Pressable Customer Success Team uses the Debug Bar. If you need assistance tracking them down, <a href=\"https:\/\/my.pressable.com\/#help\" target=\"_blank\" rel=\"noreferrer noopener\">just let us know<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"locate-the-function-executing-the-query\" data-offset=\"125\" class=\"content-heading toc-content__target\">Locate the function executing the query<\/h2>\n\n\n\n<p>In the Debug Bar, the source function for a slow query can usually be found by locating the PHP function called just before the <code>WP_Query<\/code> class in the stacktrace. For example:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/pressable.com\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-13-17.05.13.png?ssl=1\" alt=\"Locating the function executing the query in a block of code\" class=\"wp-image-12327\"\/><\/figure>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In this case, we can see that this query is taking approximately 7 seconds to run. The function that is called just before <code>WP_Query<\/code> is <code>alg_wc_pvbur_get_invisible_products<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"locate-the-file-with-the-function\" data-offset=\"125\" class=\"content-heading toc-content__target\">Locate the file with the function<\/h2>\n\n\n\n<p>If you have a backup of your site via Jetpack Security Daily, you can download the backup and search for the function on your device: &#8220;function alg_wc_pvbur_get_invisible_products&#8221;. Alternatively, Pressable&#8217;s Customer Success team can help you locate the file containing the function as well.<\/p>\n\n\n\n<p>Once you locate the function, you should see the uncached call to <code>WP_Query<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function alg_wc_pvbur_get_invisible_products( $roles = array() ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;$query = new WP_Query( alg_wc_pvbur_get_invisible_products_query_args( $roles ) );<br>&nbsp;&nbsp;&nbsp;&nbsp;return $query;<br>}<\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>When this function is called, that query will be run every single time, even though it probably doesn&#8217;t need to execute on every page load. So, on Pressable, you can use the WordPress Transients API or Object Cache to store the results of this query for a determined amount of time. In turn, this allows the query results to be called from the Object Cache, and the page will load much faster.<\/p>\n\n\n\n<p><em>Note: On Pressable, as we offer a persistent Object Cache via memcached, the Transients API pushes any calls to <code>set_transient()<\/code>, to <code>wp_cache_set()<\/code>, for example. For that reason, this example will use <code>wp_cache_set()<\/code> instead of <code>set_transient()<\/code> to be efficient. However, if you ever host your site on a server that doesn&#8217;t have a persistent object cache, choosing one or the other will make a difference. Read more about the <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_object_cache\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress Object Cache<\/a> and <a href=\"https:\/\/codex.wordpress.org\/Transients_API\" target=\"_blank\" rel=\"noreferrer noopener\">Transients API<\/a>.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"adding-queries-to-the-cache\" data-offset=\"125\" class=\"content-heading toc-content__target\">Adding queries to the cache<\/h2>\n\n\n\n<p>Adding a query to the cache isn&#8217;t as hard as you might think. Here is how you might add the query above to the cache:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1. function alg_wc_pvbur_get_invisible_products( $roles = array() ) {<br>2. &nbsp;&nbsp;&nbsp;&nbsp;$key = serialize($roles);<br>3. &nbsp;&nbsp;&nbsp;&nbsp;if ( ! $query = wp_cache_get($key) ) {<br>4. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$query = new WP_Query( <br>  alg_wc_pvbur_get_invisible_products_query_args( $roles ) );<br>5. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wp_cache_set($key,$query,'',3600);<br>6. &nbsp;&nbsp;&nbsp;&nbsp;}<br>7. &nbsp;&nbsp;&nbsp;&nbsp;return $query;<br>8. }<\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Let&#8217;s go through this line-by-line.<\/p>\n\n\n\n<p><strong>On line 1<\/strong>, the function name and its parameters stay the same. So, no changes there.<\/p>\n\n\n\n<p><strong>On line 2<\/strong>, we see our first change. When we store something in the Object Cache, we need a unique key that will allow us to later retrieve the data from the Object Cache. As we don&#8217;t know where else this function is being used \u2013 it could be used in numerous different parts of the site \u2013 the only variable we have is the <code>$roles<\/code> parameter, which may or may not be passed to the function when it&#8217;s called.<\/p>\n\n\n\n<p>By serializing <code>$roles<\/code>, we get a serialized string that will allow us to uniquely identify the data being retrieved in this function.<\/p>\n\n\n\n<p><strong>On line 3<\/strong>, we check for <code>$key<\/code>. In plain English, &#8220;If this key is not stored in the Object Cache&#8230;&#8221;<\/p>\n\n\n\n<p><strong>On line 4<\/strong>, we run the query if the <code>$key<\/code> isn&#8217;t in the cache.<\/p>\n\n\n\n<p><strong>On line 5<\/strong>, we use <code>wp_cache_set()<\/code> to store the data. First, we tell the Object Cache what the key is; then we store the query results; next we specify the cache group, if one applies (leaving it blank (<code>''<\/code>) is usually fine; and finally, we specify the amount of seconds we want this data to persist in the cache.<\/p>\n\n\n\n<p><strong>On line 7<\/strong>, we return the <code>$query<\/code> results, which were either retrieved from the <code>WP_Object_Cache<\/code> or <code>WP_Query<\/code> classes. If it&#8217;s the first time this function is running, then the results will come from the query. The next time it runs \u2013 assuming it runs before expiration \u2013 the results will come from the Object Cache.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>If you cached the query correctly, you should notice the query disappear from the <code>Queries<\/code> tab in the Debug Bar. It will then show up under the <code>$key<\/code>, whatever it was defined as, in the <code>Object Cache<\/code> tab of the Debug bar.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have a query that is taking a long time to load, more often than not that query can be cached for a limited amount of time with WordPress object cache. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":72,"template":"","meta":{"content-type":"","footnotes":""},"knowledgebase-categories":[524],"kb-tags":[],"class_list":["post-20174","knowledgebase","type-knowledgebase","status-publish","hentry","knowledgebase-categories-tutorials"],"jetpack_sharing_enabled":true,"meta_box":{"kb_updater":"1"},"_links":{"self":[{"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/knowledgebase\/20174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/knowledgebase"}],"about":[{"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/types\/knowledgebase"}],"author":[{"embeddable":true,"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/users\/1"}],"version-history":[{"count":3,"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/knowledgebase\/20174\/revisions"}],"predecessor-version":[{"id":46277,"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/knowledgebase\/20174\/revisions\/46277"}],"wp:attachment":[{"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/media?parent=20174"}],"wp:term":[{"taxonomy":"knowledgebase-categories","embeddable":true,"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/knowledgebase-categories?post=20174"},{"taxonomy":"kb-tags","embeddable":true,"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/kb-tags?post=20174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}