I've read the documentation on get_post_permalink()
and get_permalink()
and don't understand the difference between the two. It might be because I don't understand the purpose of the $leavename
and $sample
parameters. Can anyone explain those, and when one function would be more useful than the other? Thanks!
The get_post_permalink()
funciton fetches the link to a post depending on its "permanent" link plus your custom rewrite rules that changes ?p=123
into for e.g. my-beautiful-sunday-diary
. The get_permalink()
function is more "basic" but as well more versatile in what it does: For a post_type
of
page
, it usesget_page_link()
attachment
, it usesget_attachment_link()
post
, it usesget_post_link()
It also handles the display of term
s like category
and date permalinks. At the end, it either replaces the "pretty" link in your home_url()
or just returns the raw link if no custom rewrite rules were assigned. Finally it attaches a generic filter:
/**
* Filters the permalink for a post.
*
* Only applies to posts with post_type of 'post'.
*
* @since 1.5.0
*
* @param string $permalink The post's permalink.
* @param WP_Post $post The post in question.
* @param bool $leavename Whether to keep the post name.
*/
return apply_filters( 'post_link', $permalink, $post, $leavename );
Hope that clarifies the topic.
ProTip: If you need to change peramlinks in a plugin, go with the specific filters inside get_attachment_link()
, get_post_link()
, etc. Only if you are either working on a single site and are not planning to distribute your code or if you are writing a plugin targetting only rewrite stuff, then go with the generic filter above. Else you will nuke every theme authors efforts and start a callback priority race.
Someone else will certainly explain better than me. As I only use get_permalink()
They are mostly similar as they both return the post permalink, get_permalink
use get_post_permalink
(for post_types) and can be filter. It will also be use to retrieve a page link, attachment... where get_post_permalink
seems to be dedicated to post_types.
EDIT:
About the use of $leavename
, it's look like there is no need for a front-end (and even in the back-end) use as it return the permastructure slug, according to the post type of the link.
echo get_permalink(123, true);
Return the rewrite schema for the link, that could be use
A post:
http://example.com/%postname%/
A product:
http://example.com/%product%/
$leavename is use in the get_permalink() in the $rewritecode array and put as first paramater in the function line 221
$permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
It can be usefull to discover the rewrite slug for a link for a developper (but I think there a better way to do this)
Hope someone will give more details.
-
Thanks! I'm still not really clear on this though. Have you ever used $leavename to change the permalink structure? Why and how would one do that? And do you mean that get_post_permalink() is used to retrieve archives, or are you referring to get_permalink() there? I've never seen get_post_permalink() used until today when I ran across it being used exactly like get_permalink() so I'm not clear on the difference. – Michelle Nov 17 '16 at 20:56
-
I'm talking about get_permalink, you have it in the source, it retrieve author page, categories etc... It will do more than get_post_permalink. Get_permalink is a template function if I'm not mistaken, and is powerfull. – Benoti Nov 17 '16 at 21:01
-
Understood, I'm just not getting when we'd ever need to or want to use get_post_permalink() - can you give a code example? And do you know what $leavename and $sample do? I've only ever used get_permalink() as well :-) – Michelle Nov 18 '16 at 17:51
-
2
$leavename
is used in admin, to get the permalink structure so that it can generate the interface that lets you edit a post's slug. – Milo Nov 18 '16 at 19:27