Discover in this tutorial how to disable the WP_Query
cache that WordPress incorporates from version 6.1 onwards using code.
One of the new features of WordPress 6.1 is that it incorporates WP_Query
query cache. This provides a performance improvement of up to 50%, which is great because it will improve web speed in many circumstances.
But if you’re in a testing/development environment you probably don’t want this cache to apply as it can mask the changes you’re making.
Another circumstance where it may be worth avoiding the cache is when you are trying to solve a problem that may be related to WP_Query
on a production site.
In that case, you need to disable the WP_Query
cache.
The good thing is that you can do it using a WordPress hook. Here’s how.
Snippet to disable WP_Query
cache in WordPress
Add the following code snippet to your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
In this case you use the parse_query
hook to indicate that the results of the WP_Query
query should not be cached. It will be applied to all queries on the web.
If you don’t want it to affect the entire website, another option is to add a parameter within the query itself. This would be an example:
To see this and another 1097 code snippets of this website, login or subscribe here.
By setting the cache_results
parameter to false
you ensure that the query is not cached.
I reiterate that I only recommend you to do this in a test environment or apply it temporarily to solve a problem in a development site.
Conclusions
The new feature of WP_Query
queries cache is great because it substantially improves web performance.
But if you are in a testing environment or doing some debugging you may want to disable it, and now you know how.
Any questions? Let me know in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉