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?
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: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()
andget_sample_permalink()
.Since we only need link without form cruft, we can rework it into something like:
No confidence it is foolproof, but works fine from quick test. 🙂