在…内wp_delete_post()
有以下代码,
/**
* Filters whether a post deletion should take place.
*
* @since 4.4.0
*
* @param bool|null $delete Whether to go forward with deletion.
* @param WP_Post $post Post object.
* @param bool $force_delete Whether to bypass the trash.
*/
$check = apply_filters( \'pre_delete_post\', null, $post, $force_delete );
if ( null !== $check ) {
return $check;
}
因此,我认为通过以下过滤,您可能可以防止删除。
function prevent_cpt_delete( $delete, $post, $force_delete ) {
// Is it my post type someone is trying to delete?
if ( \'my_post_type\' === $post->post_type && ! $force_delete ) {
// Try to trash the cpt instead
return wp_trash_post( $post->ID ); // returns (WP_Post|false|null) Post data on success, false or null on failure.
}
return $delete;
}
add_filter(\'pre_delete_post\', \'prevent_cpt_delete\', 10, 3);