I have been using transients a lot in WordPress since I discovered them, however to ensure my sites update properly when a post is edited or created I added a function to delete transients on post save.
This works great however I need to keep a list of all the transients I create throughout the site and then mention each one by name in my function.
This is especially troublesome when I name the transients automatically, for example some transients are named based on their post title.
Is there a way in WordPress to get a list of all the transients currently created on the site? I’d like to clear them all on post save. I’d like to do this whilst avoiding directly deleting the database entries (ie I’d like to use the delete_transient() function rather than just delete all the transients directly in the database).
The db query would look like this:
To sort the results by their function (site transients, timeouts) use a function like this:
Now you get an array, separated by the transient functions with unserialized values.
Sample output:
You can query the database for all transients using something like:
Or you can install this plugin.
You can use the class
wpdb
to make a query in DB to get this info :EDIT: use prepare() method if you choose to make query in database through wpdb
If you don’t maintain a list, then there is no list. Core uses transients as well, so you’ll need some way to identify your own, with some sort of unique prefix. You could then delete them all with a SQL query, like:
I can’t speak to whether or not that’s actually a good idea. You could safely delete them all and force core and all your plugins to recreate their transients, but that could be a hefty load depending on what was involved in creating them.