WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I use the_permalink or get_the_permalink in a draft or scheduled post, the URL provided is not the "final" permalink—it is the unpretty ?p=xxxxx version.

How can I get the final, "clean" permalink to show up in a draft or scheduled post?

I could do something involving $post->post_name, but I'd need the path as well, and that varies from post type to post type and depends on permalink structure. Is there a "universal" way to do this?

share|improve this question
Why would you need the pretty permalink for drafts? – sanchothefat Feb 8 '12 at 14:49
@sanchothefat Among other things, I generate a PDF (containing the post's permalink), before publishing. Now the only way I can do that on an unpublished post is with static text. – supertrue Feb 8 '12 at 19:18
up vote 3 down vote accepted

This is a little "hacky", but when you call get_permalink and you need the permalink from a draft, provide a clone of your post object with the details filled in:

global $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();
share|improve this answer

Since editor displays projected permalink for slug editor, it must have some way to figure it out. From looking at source that is handled by get_sample_permalink_html() and get_sample_permalink().

Since we only need link without form cruft, we can rework it into something like:

function get_draft_permalink( $post_id ) {

    require_once ABSPATH . '/wp-admin/includes/post.php';
    list( $permalink, $postname ) = get_sample_permalink( $post_id );

    return str_replace( '%postname%', $postname, $permalink );

No confidence it is foolproof, but works fine from quick test. :)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.