How to create preview link of draft post with slug

Đã có trọn bộ đề thi thử THPT Quốc Gia 2022 các môn: vip.dethihsg247.com
QUẢNG CÁO: Đổi thẻ cào thành tiền mặt min rút 10k tại đây

In WordPress, the Draft post preview will have the below link: mysite.com/?p=2000&preview=true

But I want to custom this link to slug/permalink like: mysite.com/my-category/my-post-slug?preview=true (This link have only category slug and post slug, it’s the same with published post link).

I am using post_link hook and use a trick to custom preview link: make the post status publish and switch to draft immediately. When I do that, the post will create the slug (post_name in the Database).

My code below:

Result: It’s work but it cause some bugs like:

I want to filter that:

Thank you so much.

Xin chào các bạn và quý Thầy Cô. Hãy nhập từ khóa mình muốn vào ô tìm kiếm trên Website để tìm kiếm mọi thứ hoặc trên google các bạn hãy nhập từ khóa + dethihsg247.com để tìm kiếm các bài viết của chúng tôi nhé.
  • Hiện tại chúng tôi có hơn 25 nghìn bài văn mẫu các thể loại.
  • Kho tài liệu, đề thi học sinh giỏi các lớp vô cùng phong phú.
  • Mỗi ngày cập nhật hơn 100 đề thi chất lượng từ các website bán tài liệu lớn.

Tải đề thi VIP với giá siêu rẻ tại Vip.Dethihsg247.Com