You can make use of the filters supplied via the delete/trash functions of WordPress API: wp_trash_post
and wp_delete_post
:
function prevent_post_trash_delete($bool, $post){
$posts_to_keep = [30];
if ( isset( $post->ID ) && in_array($post->ID, $posts_to_keep) ) {
return false;
}
return $bool;
}
add_filter('pre_delete_post', 'prevent_post_trash_delete', 10, 2);
add_filter('pre_trash_post', 'prevent_post_trash_delete', 10, 2);
You can pass an array of IDs (so be it post, page, or any other post type) through the $posts_to_keep
variable.
That way they will throw a notice when a user tries to delete them via the dashboard. You can’t prevent plugins using direct SQL queries from deleting these pages.