You may have come across a situation where it'd be nice to share a draft post/page in WordPress with your team or a client.

This would allow them to see a live version of your page without needing to hit publish or requiring them to log in.

This can be especially useful when you want to gather feedback on a page from a client, or when you're writing a guest post and want to share the first draft from your own website.

The most common way to share draft pages in WordPress is to use a plugin like Public Post Preview.

However, you might want to keep things simple and share unpublished drafts without adding another plugin to your site.

In this post, I'll show you how to do exactly that.

The Code Snippet

To share a draft page in WordPress without a plugin, you can use the following PHP code snippet.

add_action('pre_get_posts', 'allow_draft_preview');

function allow_draft_preview($query) {
    if (isset($_GET['key']) && $_GET['key'] == 'guest') {
        if ($query->is_main_query()) {
            $query->set('post_status', array('publish', 'draft'));

You can either add it to your theme's functions.php file, or use a code snippet manager like WPCodeBox (my personal favorite).

Then to share your draft page, simply add "&key=guest" to the preview URL.

Your URL should look something similar to this:

Here's a rundown of what this snippet does:

  • The first line of code adds an action to the WordPress core called "pre_get_posts". This action is triggered when WordPress is preparing to retrieve posts from the database.
  • The second line of code defines a function called "allow_draft_preview". This function will be triggered when the pre_get_posts action is triggered.
  • The third line of code checks to see if the URL contains a query parameter called "key" with a value of "guest".
  • The fourth line of code checks to see if the query being processed is the main query.
  • The fifth line of code changes the post status to include both published and draft posts.

Of course, you'll want to test this out carefully on your website (use at your own risk).

I'd recommend using it on staging first, and always have a backup in case things go sideways.

It may also be a good idea to deactivate the snippet whenever you're not using it, just so someone can't preview one of your draft posts if they happen to know the post ID.

Wrapping Up

You should now be able to share a draft in WordPress without needing a plugin.

Let me know in the comments section below if you found this snippet useful!