⚡ Built-in Caching System
API call taking 2 seconds? Database query slow? Cache it with Transients API. No plugin needed.
Basic Usage
// Get from cache
$data = get_transient('my_cached_data');
// Not in cache? Get fresh data
if (false === $data) {
$data = expensive_api_call();
// Store for 1 hour (3600 seconds)
set_transient('my_cached_data', $data, HOUR_IN_SECONDS);
}
return $data;
Real Example: Cache WooCommerce Products
function get_featured_products() {
$transient_key = 'featured_products_v1';
$products = get_transient($transient_key);
if (false === $products) {
// Expensive query
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'meta_query' => array(
array('key' => '_featured', 'value' => 'yes')
)
);
$products = new WP_Query($args);
// Cache for 12 hours
set_transient($transient_key, $products, 12 * HOUR_IN_SECONDS);
}
return $products;
}
// Delete cache when product updated
add_action('save_post_product', function() {
delete_transient('featured_products_v1');
});
⏱️ Time Constants
MINUTE_IN_SECONDS= 60HOUR_IN_SECONDS= 3600DAY_IN_SECONDS= 86400WEEK_IN_SECONDS= 604800MONTH_IN_SECONDS= 2592000YEAR_IN_SECONDS= 31536000
“Homepage was hitting Instagram API 50 times per minute. Added transient cache (30 min expiry). Server load dropped 90%. Zero plugins.”
