How to get the clean permalink in a draft?

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?

2 s
2

This is a little “hacky”, but when you call get_permalink and you need the permalink for 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="publish";
    $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();
}

Leave a Comment