How to Link to a Draft in WordPress with Pretty Permalinks

Update: Unfortunately, the below code doesn’t work with the WordPress Gutenberg editor. If you need this badly, consider temporarily switching to the WordPress classic editor and then switching back.

The “Insert/Edit” link button in WordPress is a great tool for quickly referencing your own content. You can search by keyword and it automatically fills in the permalink. However, it has one major limitation – you can’t link to a draft post. This is important because sites often create interlinking sets of pages as drafts before publishing all of them at once. Say for example you’re writing a series of articles, each referencing the other and you want all of them as to be published at once, the WordPress link inserter won’t be of much help.

Here for example, I have a draft post on my site:

Create a Draft Post
Create a Draft Post

And when I try and link to it from the WordPress editor, nothing happens:

Draft Post Won't Appear when you Try and Link to it
Draft Post Won’t Appear when you Try and Link to it

Code for Including Draft Links

Here, I’ll give you a little snippet of code to correct the problem. Open up your functions.php or your custom PHP code inserter and paste the following:

function draft_permalink( $post) {
    if (in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) {
        $my_post = clone $post;
        $my_post->post_status = 'published';
        $my_post->post_name = sanitize_title($my_post->post_name ? $my_post->post_name : $my_post->post_title, $my_post->ID);
        $permalink = get_permalink($my_post);
    } else {
        $permalink = get_permalink();

    return $permalink;

function get_draft_permalink( $url, $post, $leavename=false ) {

	if ( $post->post_status == 'draft' )
		$url = draft_permalink($post);

	return $url;
add_filter( 'post_link', 'get_draft_permalink', 10, 3 );

If you don’t know how to insert code like this, read my earlier tutorial on WordPress code snippets.

This piece of code does two things:

  1. It allows draft posts to appear in the link inserter/editor
  2. It returns the full “pretty permalink” URL of draft posts

Without the second part, the editor will use “ugly” permalinks instead of the human readable ones your probably want. Here you can see that the suggestions for the draft posts appear in the link editor:

Draft Post Now Showing
Draft Post Now Showing

And finally, here’s the full URL that’s inserted:

Managed to link to a draft
Add Link to Draft

You can see that it’s a “pretty” permalink, and not one with the page ID. So that’s how you use get to insert draft links in WordPress! Even if you don’t want to insert these links, the code is still useful if you need to know how to obtain the pretty permalinks of draft posts.

About Bhagwad Park

I've been writing about web hosting and WordPress tutorials since 2008. I also create tutorials on Linux server administration, and have a ton of experience with web hosting products. Contact me via e-mail!


  1. Hello, I installed the custom-plugin by following your other tutorials, but when I copy+pasted the code into the space indicated, I’m still not able to search and find posts that are drafts! Please let me know what I can do!

    Thanks for this post by the way! ๐Ÿ™‚


    • WPTweaks Admin says

      Hi Dee. That’s strange, it should work. Is the custom plugin activated? Could you test it out by pasting some other code into the custom plugin?

      Alternatively, you can try pasting the code into functions.php instead. Does it work over there?


  2. Did this stop working in some version of WP 4.9 (or earlier)? I’ve added this to a plugin as well as my functions.php and in neither case does it show draft posts.

    Alternatively, is there a way to add logging info so that I can see that the code is being run and behaving as expected?


  3. Please update tutorial for late 2019 ๐Ÿ˜‰ I desperately need this function to work ๐Ÿ˜‰


  4. Just adding to the chorus – this isn’t working in the current WordPress, and it would be nice to have it.


  5. It’s actually working beautifully for me!

    class DraftPermalinks
    * Init
    public static function init(): void
    add_filter(‘post_type_link’, [__CLASS__, ‘draft_permalink’], 10, 3);
    add_filter(‘page_link’, [__CLASS__, ‘draft_permalink’], 10, 3);

    * Allow pretty permalinks for drafts
    * @see
    public static function draft_permalink(
    string $permalink,
    int|\WP_Post $post,
    bool $leavename
    ): string {
    /** for filter recursion (prevent infinite loop) */
    static $recursing = false;

    $post = get_post($post);

    if (!is_user_logged_in()) {
    return $permalink;

    /** set the post status to publish to get the ‘publish’ permalink */
    if (!$recursing && in_array($post->post_status, [‘draft’, ‘pending’, ‘auto-draft’])) {
    $clone = clone $post;
    $clone->post_status = ‘publish’;
    if (empty($clone->post_name)) {
    $clone->post_name = sanitize_title($clone->post_title, $clone->ID);
    $recursing = true;
    return get_permalink($clone, $leavename);

    $recursing = false;
    return $permalink;


Speak Your Mind