How to Link to a Draft in WordPress with Pretty Permalinks

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:

draft-post

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

draft-post-not-appearing

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:

now-appearing-in-list

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

Managed to link to a 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.

Comments

  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! 🙂

    Reply

    • 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?

      Reply

Speak Your Mind

*