要移除挂钩,它必须具有与添加挂钩时相同的优先级。添加优先级为1的\\u操作。如果要删除该操作,则需要以1的优先级删除它。看见the codex.
add_action( \'save_post\', \'change_post_status\', 1 );
function change_post_status( $post_id ){
$my_post = array(
\'ID\' => 1,
\'post_status\' => \'draft\',
);
// unhook this function, making sure to use the same priority, so it doesn\'t loop infinitely
remove_action(\'save_post\', \'change_post_status\', 1 );
if( $post_id == 1 ){
wp_update_post( $my_post );
}
// re-hook this function with the initial priority
add_action(\'save_post\', \'change_post_status\', 1 );
}