# get_permalink()

URL: https://chugunov.pro/api-wordpress/functions/get_permalink/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 1.0.0.

## Сигнатура

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

## Описание

Извлекает полную постоянную ссылку для текущей записи или по ID записи.

## Параметры

- `$post` `int|WP_Post` — необязательный. ID записи или объект записи. По умолчанию — глобальная переменная $post.
- `$leavename` `bool` — необязательный, по умолчанию `false`. Сохранять ли имя записи или имя страницы.

## Возвращаемое значение

`string|false`

## Исходный код

Файл: `wp-includes/link-template.php:170`

```php
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;
				}
			}
			/*
			 * Show default category in permalinks,
			 * without having to assign it explicitly.
			 */
			if ( empty( $category ) ) {
				$default_category = get_term( get_option( 'default_category' ), 'category' );
				if ( $default_category && ! is_wp_error( $default_category ) ) {
					$category = $default_category->slug;
				}
			}
		}

		$author = '';
		if ( str_contains( $permalink, '%author%' ) ) {
			$authordata = get_userdata( $post->post_author );
			$author     = $authordata->user_nicename;
		}

		/*
		 * This is not an API call because the permalink is based on the stored post_date value,
		 * which should be parsed as local time regardless of the default PHP timezone.
		 */
		$date = explode( ' ', str_replace( array( '-', ':' ), ' ', $post->post_date ) );

		$rewritereplace = array(
			$date[0],
			$date[1],
			$date[2],
			$date[3],
			$date[4],
			$date[5],
			$post->post_name,
			$post->ID,
			$category,
			$author,
			$post->post_name,
		);

		$permalink = home_url( str_replace( $rewritecode, $rewritereplace, $permalink ) );
		$permalink = user_trailingslashit( $permalink, 'single' );

	} else { // If they're not using the fancy permalink option.
		$permalink = home_url( '?p=' . $post->ID );
	}

	/**
	 * 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 );
}
```

## История изменений

- 1.0.0 — Introduced.

## Связанные

Использует: [`wp_force_plain_post_permalink`](https://chugunov.pro/api-wordpress/functions/wp_force_plain_post_permalink/), [`wp_list_sort`](https://chugunov.pro/api-wordpress/functions/wp_list_sort/), [`get_the_category`](https://chugunov.pro/api-wordpress/functions/get_the_category/), [`get_category_parents`](https://chugunov.pro/api-wordpress/functions/get_category_parents/), [`get_page_link`](https://chugunov.pro/api-wordpress/functions/get_page_link/), [`get_attachment_link`](https://chugunov.pro/api-wordpress/functions/get_attachment_link/), [`get_post_permalink`](https://chugunov.pro/api-wordpress/functions/get_post_permalink/), [`user_trailingslashit`](https://chugunov.pro/api-wordpress/functions/user_trailingslashit/), [`get_userdata`](https://chugunov.pro/api-wordpress/functions/get_userdata/), [`get_term`](https://chugunov.pro/api-wordpress/functions/get_term/), [`home_url`](https://chugunov.pro/api-wordpress/functions/home_url/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), [`get_option`](https://chugunov.pro/api-wordpress/functions/get_option/), [`get_post`](https://chugunov.pro/api-wordpress/functions/get_post/), [`get_post_types`](https://chugunov.pro/api-wordpress/functions/get_post_types/), [`is_wp_error`](https://chugunov.pro/api-wordpress/functions/is_wp_error/).
Используется в: [`_block_bindings_post_data_get_value`](https://chugunov.pro/api-wordpress/functions/_block_bindings_post_data_get_value/), `WP_Sitemaps_Posts::get_url_list`, `WP_REST_Post_Search_Handler::prepare_item`, [`get_privacy_policy_url`](https://chugunov.pro/api-wordpress/functions/get_privacy_policy_url/), `WP_REST_Posts_Controller::prepare_item_for_response`, `WP_REST_Posts_Controller::get_item`, `WP_Customize_Nav_Menus::ajax_insert_auto_draft_post`, [`wp_get_canonical_url`](https://chugunov.pro/api-wordpress/functions/wp_get_canonical_url/), [`wp_embed_excerpt_more`](https://chugunov.pro/api-wordpress/functions/wp_embed_excerpt_more/), [`wp_oembed_add_discovery_links`](https://chugunov.pro/api-wordpress/functions/wp_oembed_add_discovery_links/), [`get_post_embed_url`](https://chugunov.pro/api-wordpress/functions/get_post_embed_url/), [`get_post_embed_html`](https://chugunov.pro/api-wordpress/functions/get_post_embed_html/), [`get_preview_post_link`](https://chugunov.pro/api-wordpress/functions/get_preview_post_link/), `WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item`, `WP_Post`, `WP_Customize_Nav_Menus::search_available_items_query`, `WP_Customize_Nav_Menus::load_available_items_query`, `WP_Posts_List_Table::handle_row_actions`, [`wp_install_maybe_enable_pretty_permalinks`](https://chugunov.pro/api-wordpress/functions/wp_install_maybe_enable_pretty_permalinks/), [`wp_dashboard_recent_posts`](https://chugunov.pro/api-wordpress/functions/wp_dashboard_recent_posts/), [`get_media_item`](https://chugunov.pro/api-wordpress/functions/get_media_item/), [`get_sample_permalink_html`](https://chugunov.pro/api-wordpress/functions/get_sample_permalink_html/), [`get_sample_permalink`](https://chugunov.pro/api-wordpress/functions/get_sample_permalink/), `WP_Media_List_Table::_get_row_actions`, `WP_Comments_List_Table::column_response`, [`wp_list_categories`](https://chugunov.pro/api-wordpress/functions/wp_list_categories/), [`wp_notify_postauthor`](https://chugunov.pro/api-wordpress/functions/wp_notify_postauthor/), [`wp_notify_moderator`](https://chugunov.pro/api-wordpress/functions/wp_notify_moderator/), [`wp_get_archives`](https://chugunov.pro/api-wordpress/functions/wp_get_archives/), [`get_boundary_post_rel_link`](https://chugunov.pro/api-wordpress/functions/get_boundary_post_rel_link/), [`get_parent_post_rel_link`](https://chugunov.pro/api-wordpress/functions/get_parent_post_rel_link/), [`previous_post`](https://chugunov.pro/api-wordpress/functions/previous_post/), [`next_post`](https://chugunov.pro/api-wordpress/functions/next_post/), [`wp_old_slug_redirect`](https://chugunov.pro/api-wordpress/functions/wp_old_slug_redirect/), [`get_comments_pagenum_link`](https://chugunov.pro/api-wordpress/functions/get_comments_pagenum_link/), [`paginate_comments_links`](https://chugunov.pro/api-wordpress/functions/paginate_comments_links/), [`get_adjacent_post_link`](https://chugunov.pro/api-wordpress/functions/get_adjacent_post_link/), [`get_adjacent_post_rel_link`](https://chugunov.pro/api-wordpress/functions/get_adjacent_post_rel_link/), [`get_post_type_archive_link`](https://chugunov.pro/api-wordpress/functions/get_post_type_archive_link/), [`get_post_comments_feed_link`](https://chugunov.pro/api-wordpress/functions/get_post_comments_feed_link/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/get_permalink/
