Learn how to use WordPress transients to cache expensive database queries. Boost your plugin's speed and reduce database load with proper invalidation logic.
Previously in this course, we explored advanced database queries to fetch complex datasets. While efficient SQL is crucial, the fastest query is the one you never have to run.
In this lesson, we introduce caching to your Knowledge Base plugin. By storing the results of expensive database operations in temporary memory, we can significantly improve your site's speed and reduce database strain.
Transients are the WordPress API for storing cached data. They are essentially a wrapper around the wp_options table (or an external object cache like Redis or Memcached if configured). When you "set a transient," you are saving a value associated with a unique key, optionally with an expiration time.
Think of it as a short-term memory store. If the data is available in the transient, we use it. If not, we run our database query, save the result to the transient, and then use it.
get_transient and set_transientTo implement this in our KnowledgeBaseModel, we follow a "check-then-set" pattern. We check for the existence of a specific cache key first.
In our Knowledge Base plugin, let’s say we frequently display the total count of published articles in a sidebar widget. Instead of running SELECT COUNT(*) FROM wp_posts... on every page load, we cache it.
PHP#6A9955">// Inside KnowledgeBaseModel.php public function get_article_count() { $cache_key = 'kb_article_count'; #6A9955">// 1. Attempt to get the value from the cache $count = get_transient($cache_key); #6A9955">// 2. If it's false, the cache is empty or expired if (false === $count) { global $wpdb; $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'kb_article' AND post_status = 'publish'"); #6A9955">// 3. Set the transient for 12 hours(43200 seconds) set_transient($cache_key, $count, 12 * HOUR_IN_SECONDS); } return $count; }
The biggest risk with caching is serving stale data. If an admin publishes a new article, the kb_article_count will still show the old number until the 12-hour expiration passes. We must implement cache invalidation to delete the transient whenever the data changes.
We hook into the save_post_kb_article action to clear the cache:
PHP#6A9955">// Inside KnowledgeBaseModel.php or a dedicated Service class public function clear_article_count_cache() { delete_transient('kb_article_count'); } #6A9955">// In your plugin's init or constructor: add_action('save_post_kb_article', [$this, 'clear_article_count_cache']);
By calling delete_transient whenever an article is saved, updated, or deleted, we ensure the next request triggers a fresh database query, keeping our performance gains without sacrificing accuracy.
KnowledgeBaseModel.php.SELECT query (e.g., fetching a list of categories or recent articles).get_transient check.set_transient call to cache the results for 1 hour.save_post, edited_term) correspond to the data you are caching and add delete_transient calls to those hooks to invalidate the cache.kb_user_stats_{$user_id}).set_transient with a corresponding delete_transient in your update/delete hooks.Transients provide a high-level API to manage temporary data storage. By using get_transient to verify data existence and set_transient to store results, you avoid redundant database hits. Remember, effective caching is only as good as your invalidation strategy; always ensure that updates to your content trigger a cache clear.
Up next: We will discuss plugin security best practices to ensure that our cached data and database interactions remain protected against modern web threats.
Master advanced database queries in WordPress. Learn to write custom SQL JOINs with $wpdb and profile query performance to build scalable, high-speed plugins.
Read moreMaster WordPress security by implementing capability checks. Learn to use current_user_can to restrict admin features and enforce proper access control.
Caching Strategies
Plugin Deployment Strategy
Advanced MVC: Dependency Injection
Handling Large Datasets
Error Handling and Logging