在创建,编辑或删除新产品时,我可以删除一些瞬态设置,但是如果它是管理面板上的批量操作(例如批量编辑或移至回收站),则无法完成。
我已经使用了动作挂钩'transition_post_status',并且每当从旧状态更改为新状态时,或者如果是更新方案,都会执行我的瞬时删除代码。但这不适用于批量编辑或批量移至回收站。
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 );
当我为新产品创建,更新或移入垃圾桶时,我将所有瞬态删除了,但是在进行批量操作后,什么也没发生。
答案 0 :(得分:0)
此基于Wordpress custom post action hook的解决方案行之有效,但在批量删除帖子的情况下效果不佳,因为我代码中的任何批量删除都将$ post设为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);
}