It’s been so many years I started playing with WordPress. On Crunchify we do have more than 500 tricks and tips on how to modify WordPress functionality as per your site need.
In this tutorial we will discuss one more utility which may help some of you just incase if you want to remove _transient_
option programmatically after each post publish.
Also, I got a question on one of my post before.
What are _transient_ variable in wp_options table in WordPress?
_transient_
objects are the variable which stores or caches temporary values with defined expiry date and time. All _transient_ options are stored in wp_options table.
This table is used by default WordPress core functionality and by any plugin or theme developer
who want to store temporary values
with provided expired timestamp.
Simple example:
Consider sitemap.xml
file. You don’t want to keep querying Database table each time you want to see Sitemap. It may create high load on server generating Sitemap.xml file. Also, Google Search Console may query sitemap any time during a day and may be multiple times we don’t know.
Why not to just create a sitemap.xml once and cache it as _transient_ object and only update it if required like new post / page publish or edit operation.
Can I remove transients in the wp_options table?
Yes. It’s 100% safe to dump all Transient Object from DB.
How to Manage and Delete Transients in WordPress
Here is a simple function
which will remove all _transient_ objects
from wp_options
table.
function crunchify_remove_transient_on_publish( $new, $old, $post ) { if( 'publish' == $new ) delete_transient( 'recent_posts_query_results' ); } add_action('transition_post_status', 'crunchify_remove_transient_on_publish', 10, 3 );
If you want to remove transient object from Database directly then execute below query. It’s always good practice to backup your DB before performing any operation.
delete from `wp_options` where `option_name` like '%_transient_%'
If you prefer to use Plugin for this operation then you could use WordPress Plugin Transients Manager.
Let me know if you have any more condition or variation which you want to apply.