Upon creating, editing or deleting a new product, I'm able to delete some set of transients but couldn't get it done if it's a bulk action at the admin panel like bulk edit or move to trash.
I've used the action hook 'transition_post_status' and whenever there is a change from old status to a new status or if it's an update scenario then my transient deleting code gets executed. But this doesn't work for bulk edit or bulk move to trash.
function ga_delete_transients( $new_status, $old_status, $post ) {//deletes transients if exists upon create,trash and update
global $post,$wpdb;
if ( $post->post_type !== 'product' ) return;
if($old_status!==$new_status|| $old_status == 'publish' && $new_status =='publish'){
$sql = "SELECT * FROM labtag_wp.wp_labtag_options WHERE `option_name` LIKE ('%\_transient\_ga_loop_products_%')";
$ga_transient_result = $wpdb->get_results($sql);
if(!empty($ga_transient_result)){
$wpdb->query( "DELETE FROM labtag_wp.wp_labtag_options WHERE `option_name` LIKE ('%\_transient\_ga_loop_products_%')" );
}
}
}
add_action( 'transition_post_status', 'ga_delete_transients', 10, 3 );
I get all the transients deleted when it's creating, updating or moving to trash for a new product but upon bulk actions, nothing happens.
This solution based on Wordpress custom post action hook works but isn't good in case of a bulk delete of posts as any bulk delete within my code will have $post to be null.
function ga_delete_transients($post) {
global $wpdb,$post;
if(isset($post)&&$post->post_type=='product' || !isset($post) ) {//doesn't work in case of bulk delete of posts
$sql = "SELECT * FROM labtag_wp.wp_labtag_options WHERE `option_name` LIKE ('%\_transient\_ga_loop_products_%')";
$ga_transient_result = $wpdb->get_results($sql);
if(!empty($ga_transient_result)){
$wpdb->query( "DELETE FROM labtag_wp.wp_labtag_options WHERE `option_name` LIKE ('%\_transient\_ga_loop_products_%')" );
}
}
else{
return;
}
}
$all_actions = array('save_post','wp_delete_post','wp_trash_post');
foreach ($all_actions as $current_action) {
add_action($current_action, 'ga_delete_transients',1);
}