Cache Remote API Calls with the WordPress Transients API
Reduce external requests and database load by caching API responses with WordPress transients, plus key limits and verification steps.
20 Feb 2026, 13:29 UTC

Your weather widget is calling an API on every page load
A custom widget fetches current conditions from an external weather service every time a visitor loads a page. On a busy site that means hundreds of remote requests per hour, slower page rendering, and a real chance of hitting the provider's rate limit. You need to store the response temporarily and reuse it until the data is allowed to go stale.
Useful takeaway: WordPress transients let you cache a value with an expiration time. When the cache is valid, get_transient() returns the stored data without touching the database or the remote API. When it expires or is missing, you fetch fresh data and store it again with set_transient().
Where transients live and how they expire
A transient is a named value stored with a lifetime in seconds. Without a persistent object cache, WordPress keeps transients in the wp_options database table, using rows that begin with _transient_ for the value and _transient_timeout_ for the expiration timestamp. With an object cache such as Redis or Memcached, the same API calls read from memory instead.
Expiration is advisory. WordPress does not delete a transient at the exact second it expires. It removes the value the next time someone calls get_transient() or during garbage collection. That means a transient can linger briefly past its deadline, so it is not suitable for data that must change immediately.
Pick safe data and namespaced keys
- Good candidates: RSS feeds, external API responses, complex
WP_Queryresults that change infrequently. - Never store: passwords, authentication tokens, or personally identifiable information. Transients are not encrypted by default.
- Key limit: 172 characters. Prefix every key with your plugin or theme slug to avoid collisions.
Worked example: cache a weather API response
Add this to a custom plugin or your theme's functions.php. It checks the cache first, calls the remote API only on a miss, and stores the result for one hour.
<?php
function myplugin_get_weather( string $city ) {
$key = 'myplugin_weather_' . sanitize_key( $city );
$cached = get_transient( $key );
if ( false !== $cached ) {
return $cached;
}
$response = wp_remote_get(
'https://api.example.com/weather?city=' . urlencode( $city )
);
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return new WP_Error( 'api_failed', 'Weather service unavailable.' );
}
$data = json_decode( wp_remote_retrieve_body( $response ), true );
// 3,600 seconds = 1 hour. Expiration is advisory, not instant.
set_transient( $key, $data, 3600 );
return $data;
}
Call myplugin_get_weather( 'London' ) in your template. The first request hits the API; subsequent requests within the hour return the cached value.
Verify behavior and watch for bloat
Run these checks on a staging site, not production.
Version assumption: these functions exist in current WordPress core. If you maintain an older or heavily modified fork, confirm set_transient, get_transient, and delete_transient exist in wp-includes/option.php.
Command-line inspection with WP-CLI
Where to run: your terminal, inside the WordPress root directory. Required permissions: SSH access as a user who can read WordPress files and the database credentials in wp-config.php.
# Check whether a transient exists in the database
wp option get _transient_myplugin_weather_london
# Remove a transient manually. Use with care; a mistyped key deletes the wrong cache.
wp option delete _transient_myplugin_weather_london
Expected check: the first command returns the cached value or an empty response if the key is absent. Risk: deleting the wrong key forces an unnecessary refetch but does not damage core data.
Database and object-cache checks
- Inspect
wp_optionsfor rows beginning with_transient_or_transient_timeout_to confirm database storage. - Look for
wp-content/object-cache.phporadvanced-cache.php. If either exists, transients likely bypass the database and live in memory. - On staging, set a transient with a 10-second expiration, read it back, wait, and confirm
get_transient()returnsfalse.
Limitations
- Without an object cache, many large transients can bloat
wp_optionsand slow option lookups. - Transients are not a distributed lock or cache invalidation signal. Do not rely on them for immediately consistent data.
- Because values are not encrypted, keep secrets out of them entirely.
Closing
Audit your theme and plugins for remote calls or heavy queries that run on every page. Wrap one expensive call in a transient, verify it with WP-CLI or wp_options, and monitor the options table size. The result is fewer external requests and faster page loads without installing an additional caching plugin.