I’ve never used the Transients API before and was wondering if anyone has guidance on when to use it. The Codex article implies that as a theme developer I might want to set each new
WP_Query() as a transient; I assume the same might be said for direct $wpdb queries and
query_posts(). Is that overkill? And/Or are there other places I should default to using it?
I do often use caching plugins on my site (W3 Total Cache usually) and it sounds like using Transients might bump up the plugin’s effectiveness but I don’t want to go crazy wrapping everything in transients if that’s not a best practice.
Transients are great when you’re doing complex queries in your themes and plugins. I tend to use transients for things like menus and showing other things like Tweets from Twitter in a sidebar for example. I wouldn’t use them for absolutely everything more-so just temporary pieces of data that can be cached.
Keep in mind that if you use something like Memcached with transients, then you will notice a massive performance gain. The rule with transients is to not use them for data that should not expire as they’re really only for temporary data and keep in mind transients are not always stored in the database.
Some uses for transients:
- Complex and custom database queries
- WordPress navigation menus
- Sidebar widgets that display info like; tweets, a list of recent site visitors or a Flickr photo stream
- Caching tag clouds
This article is a very informative one with quick benchmarks showing just how transients can speed up your site and even has a couple of examples. This other article also has a few great examples of using transients which might help you understand what to use them for as well.