So I have created this widget which pulls a number of posts. And the query is being cached via transients. But I need the transient to be deleted whenever a post is saved so the query can be refreshed.
Have a look at this code snippet.
add_action( 'save_post', 'delete_transient' );
function delete_transient() {
if ( $_POST['post_type'] === 'post' ) {
// this is a hack around to delete widget transients
$max = 20;
for( $i = 1; $i <= $max; $i++ ) {
delete_transient( 'my-posts-widget-' . $i );
}
}
}
Ok so everything setup and working however because a widget can have multiple instances, deleting the transient is becoming an issue. Here is why. Every time you put a widget into the sidebar, the unique number WP assigns is incremented. So if someone happened to drag the widget out and then put it back again, it will increment the number again. So my $max = 20 will soon not be valid anymore as the id could then be 30... I could set the $max to 100000+ but that is not practical.
So with that in mind, you can see this is not a fully working solution. So my question is how are you guys handling this or have an idea of a work around?
Thanks for looking.