get_post_permalink( int|WP_Post $post = 0, bool $leavename = false, bool $sample = false ): string|false
- Since
- 3.0.0, 6.1.0
- Source
wp-includes/link-template.php:324
Compatibility
- WordPress
- since 6.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$postint|WP_Postoptional- Post ID or post object. Default is the global
$post.Default:0 $leavenamebooloptional- Whether to keep post name. Default false.Default:
false $samplebooloptional- Is it a sample permalink. Default false.Default:
false
Return value
string|false- The post permalink URL. False if the post does not exist.
Performance profile
How much work a call to get_post_permalink() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 10–62
- Plugin surface
- 1 hook
- Called by
- 1
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 88.
Third-party callbacks on 'post_type_link' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 7 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_post_permalink() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10 | get_post() |
!$post_type && !empty($post_link) | 48–56 | get_post(), ->get_extra_permastruct(), wp_force_plain_post_permalink(), get_post_type_object(), user_trailingslashit(), home_url(), apply_filters() |
$post_type && !empty($post_link) | 52–60 | get_post(), ->get_extra_permastruct(), wp_force_plain_post_permalink(), get_post_type_object(), get_page_uri(), user_trailingslashit(), home_url(), apply_filters() |
!$post_type | 53–58 | get_post(), ->get_extra_permastruct(), wp_force_plain_post_permalink(), get_post_type_object(), post_type(), home_url(), apply_filters() |
isset($post) | 56–58 | get_post(), ->get_extra_permastruct(), wp_force_plain_post_permalink(), get_post_type_object(), add_query_arg(), home_url(), apply_filters() |
$post_type | 57–62 | get_post(), ->get_extra_permastruct(), wp_force_plain_post_permalink(), get_post_type_object(), get_page_uri(), post_type(), home_url(), apply_filters() |
$post_type && isset($post) | 60–62 | get_post(), ->get_extra_permastruct(), wp_force_plain_post_permalink(), get_post_type_object(), get_page_uri(), add_query_arg(), home_url(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 88 | 10–62 | 9 | |
| 8.5 | 88 | 10–62 | 9 | |
| 8.4 | 88 | 10–62 | 9 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 91 | 10–63 | 9 | |
| 8.2 | 91 | 10–63 | 9 | |
| 8.1 | 91 | 10–63 | 9 | |
| 7.4 | 91 | 10–63 | 9 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while get_post_permalink() runs, in this order:
- apply_filters( post_type_link )filterline 375 (+51 into the body)
Filters the permalink for a post of a custom post type.
Uses · 8
- get_post()Retrieves post data given a post ID or post object.
- wp_force_plain_post_permalink()Determine whether post should always use a plain permalink structure.
- get_post_type_object()Retrieves a post type object by name.
- get_page_uri()Builds the URI path for a page.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- user_trailingslashit()Retrieves a trailing-slashed string if the site is set for adding trailing slashes.
- add_query_arg()Retrieves a modified URL query string.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 1
- get_permalink()Retrieves the full permalink for the current post or post ID.
Source code
function get_post_permalink( $post = 0, $leavename = false, $sample = false ) { global $wp_rewrite; $post = get_post( $post ); if ( ! $post ) { return false; } $post_link = $wp_rewrite->get_extra_permastruct( $post->post_type ); $slug = $post->post_name; $force_plain_link = wp_force_plain_post_permalink( $post ); $post_type = get_post_type_object( $post->post_type ); if ( $post_type->hierarchical ) { $slug = get_page_uri( $post ); } if ( ! empty( $post_link ) && ( ! $force_plain_link || $sample ) ) { if ( ! $leavename ) { $post_link = str_replace( "%$post->post_type%", $slug, $post_link ); } $post_link = home_url( user_trailingslashit( $post_link ) ); } else { if ( $post_type->query_var && ( isset( $post->post_status ) && ! $force_plain_link ) ) { $post_link = add_query_arg( $post_type->query_var, $slug, '' ); } else { $post_link = add_query_arg( array( 'post_type' => $post->post_type, 'p' => $post->ID, ), '' ); } $post_link = home_url( $post_link ); } /** * Filters the permalink for a post of a custom post type. * * @since 3.0.0 * * @param string $post_link The post's permalink. * @param WP_Post $post The post in question. * @param bool $leavename Whether to keep the post name. * @param bool $sample Is it a sample permalink. */ return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );}Changelog
Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/link-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.