wppaste
WordPress

get_permalink( int|WP_Post $post = 0, bool $leavename = false ): string|false

Since
1.0.0
Source
wp-includes/link-template.php:170
Retrieves the full permalink for the current post or post ID.

Parameters

$postint|WP_Postoptional
Post ID or post object. Default is the global $post.Default: 0
$leavenamebooloptional
Whether to keep post name or page name. Default false.Default: false

Return

string|false
The permalink URL. False if the post does not exist.

Hooks fired · 3

3 hooks fire while get_permalink() runs, in this order:

  1. apply_filters( pre_post_link )filterline 217 (+47 into the body)

    Filters the permalink structure for a post before token replacement occurs.

  2. apply_filters( post_link_category )filterline 244 (+74 into the body)

    Filters the category that gets used in the %category% permalink token.

  3. apply_filters( post_link )filterline 308 (+138 into the body)

    Filters the permalink for a post.

Uses · 17

Show all 17

Used by · 50

Show all 50

Source

function get_permalink( $post = 0, $leavename = false ) {	$rewritecode = array(		'%year%',		'%monthnum%',		'%day%',		'%hour%',		'%minute%',		'%second%',		$leavename ? '' : '%postname%',		'%post_id%',		'%category%',		'%author%',		$leavename ? '' : '%pagename%',	); 	if ( is_object( $post ) && isset( $post->filter ) && 'sample' === $post->filter ) {		$sample = true;	} else {		$post   = get_post( $post );		$sample = false;	} 	if ( empty( $post->ID ) ) {		return false;	} 	if ( 'page' === $post->post_type ) {		return get_page_link( $post, $leavename, $sample );	} elseif ( 'attachment' === $post->post_type ) {		return get_attachment_link( $post, $leavename );	} elseif ( in_array( $post->post_type, get_post_types( array( '_builtin' => false ) ), true ) ) {		return get_post_permalink( $post, $leavename, $sample );	} 	$permalink = get_option( 'permalink_structure' ); 	/**	 * Filters the permalink structure for a post before token replacement occurs.	 *	 * Only applies to posts with post_type of 'post'.	 *	 * @since 3.0.0	 *	 * @param string  $permalink The site's permalink structure.	 * @param WP_Post $post      The post in question.	 * @param bool    $leavename Whether to keep the post name.	 */	$permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename ); 	if (		$permalink &&		! wp_force_plain_post_permalink( $post )	) { 		$category = '';		if ( str_contains( $permalink, '%category%' ) ) {			$cats = get_the_category( $post->ID );			if ( $cats ) {				$cats = wp_list_sort(					$cats,					array(						'term_id' => 'ASC',					)				); 				/**				 * Filters the category that gets used in the %category% permalink token.				 *				 * @since 3.5.0				 *				 * @param WP_Term  $cat  The category to use in the permalink.				 * @param array    $cats Array of all categories (WP_Term objects) associated with the post.				 * @param WP_Post  $post The post in question.				 */				$category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post ); 				$category_object = get_term( $category_object, 'category' );				$category        = $category_object->slug;				if ( $category_object->parent ) {					$category = get_category_parents( $category_object->parent, false, '/', true ) . $category;

History

Introduced in 1.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.