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
Retrieves the permalink for a post of a custom post type.
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
string|false- The post permalink URL. False if the post does not exist.
Hooks 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
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 );}History
Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
6.1.0
Returns false if the post does not exist.from the docblock
3.0.0
Introduced.from the docblock
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.