The best way to do that is with this plugin:
https://wordpress.org/plugins/public-post-preview/
OF course you can dig into it’s source code to try to implement the core functionality if you prefer, but I use that plugin on several active sites with success.
Related Posts:
- Can Permalink structure of %postname%/%post_id% improve performance
- How to link post titles on page A to its posts on page B?
- Problems with WordPress in subfolder, posts show a blank page
- how to create a sub section in posts
- Categories Listing and Highlighting current category item
- Standalone form does not post to the url when form fields are populated (otherwise it does)
- Changing permalink of WordPress
- Rewriting blog permalinks
- Set Default Post as Parent Child (Nested) Relation
- How can I add random alphabets in wordpress permalink posts structure
- Change permalinks for posts & for custom post types
- Show Last Revision Of Post That Has A Published Status On Single Page
- How can I restore posts from ‘trash’ with their previous post_status? – WordPress
- Remove subcategory slug from url
- Can I show category name in url for only one of my categories?
- Permalink Short code showing unnecessary link text inside the loop
- How to set a Post’s default visibility to ‘Private’ in Gutenberg?
- Please help a newbie with blog page issue?
- My post permalinks are redirecting to the post archive page for some reason?
- How can I make a post sub link?
- Updating post status via rest api
- How do you change the permalink for posts for a single category?
- Private post caught in endless redirect loop
- How to set the post permalink using the WordPress REST API?
- Does having category name in permalinks affect SEO when having a post in multiple categories?
- WP posts using index.php instead of single.php
- How to add a ‘News’ section to specific posts in WordPress
- unable to display two post per page in wordpress front-page.php
- How to 301 redirect from url with post id to permalink with post name (slug)?
- Add a url parameter to the current post and read it in a shortcode
- How publish post from pending status
- Hide or remove custom post status
- Put page on Archived statut after end of publication date
- get_next_post() not working with future post status
- How can I set the permalink of an article / page equal to the permalink of an existing article / page?
- Prevent Archive URLs
- Custom Post Types and Broken Permalinks
- Custom permalink for wordpress page / post
- Permalink structure for two seperate blogs
- How to add category base prefix to wordpress permalink
- Category URL to use same string as Post URL Permalink
- My post permalinks are always the same
- Permalink base only for posts
- Change the URL of posts by category
- Modify loop to include all post statuses not just ‘published’
- How to give priority to page than to posts permalink structure?
- Append a random string to a post permalink
- Point users to a specific url directory for some posts
- Precedence of page permalinks over woocommerce product category links?
- Post & Category Archives URL structure
- Fixing the WP Post Object for Custom Route
- post_name is not stored until post is published… where should I store my slug until it goes live then?
- All my posts don’t show, only the headline is displayed on the homepage
- How to change post template via url?
- Posts in Page – How to customize the URL?
- Function/filter or plugin to change post status based on custom field value
- Is there a way to change the complete URL of the blog post for a redirecting tracking link?
- WordPress with static posts page and custom permalink structure doesn’t find posts with numeric permalinks
- Use a custom post URL that is outside the blog root directory
- Can’t seem to replicate permalink structure for localhost development site
- WordPress rewrite url help needed
- Permalink of some posts change
- My posts section for logged in user
- Change link to permalink inside post content
- One of my posts is displayed as page
- Make assigning post to a specific category equivalent to assigning it to all categories
- How to mix pages and sub-categories in a url
- Modifying the permalink href for posts with jquery
- Changing permalink of custom category
- TCPDF and permalinks
- Overwrite URL on blog posts
- WordPress sub-posts and permalinks
- How to display subcategory articles in the category view?
- Change posts URL [duplicate]
- 301 Redirecting posts without %postid% (just %post_name%) in permalink to ones with %post_name%_%postid%
- How do I add /blog/ on my permalink without affecting the portfolio project types permalink?
- Permalink remain the same on each page
- get_posts includes “auto drafts”?
- How can we pass a value from the_permalink() at WordPress?
- I am creating a front end dashboard where I need to show all the posts by the current user. So, I need to show posts in publish
- Read more link not working [closed]
- After the local installation of an old WP website I can see the homepage but I can’t access to the articles, why?
- Default post type doesn’t display in url
- Post Format Status [closed]
- Do anything on post_status change [duplicate]
- Changes done in admin panel are taking time to reflect in the REST api responses
- How to change Permalinks only for Blogposts
- Permalink structure for two separate blogs
- Blog posts are not appearing as subs of Blog Page
- How can I fixe the article title problem?
- How to add paraent in Blog post URL in wordpress
- Change permalinks in posts via SQL
- Why can posts never have a number as the link?
- Modify Post URL Programmatically
- editPost without undo entry
- Custom permalink for ‘post’ not working
- Permalink issue with new blog posts > getting 301 redirect
- Post type gives 404 unless logged in
- REST API – Retrieving posts with custom status
- Change the slug of post type using custom code