# get_page_by_path()

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

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

## Сигнатура

```php
get_page_by_path( string $page_path, string $output = OBJECT, string|array $post_type = 'page' ): WP_Post|array|null
```

## Описание

Возвращает страницу по её пути.

## Параметры

- `$page_path` `string` — обязательный. Путь страницы.
- `$output` `string` — необязательный, по умолчанию `OBJECT`. Требуемый тип возвращаемого значения. Одно из OBJECT, ARRAY_A или ARRAY_N, что соответствует объекту WP_Post, ассоциативному массиву или числовому массиву соответственно.
- `$post_type` `string|array` — необязательный, по умолчанию `'page'`. Тип записи или массив типов записей. Значение по умолчанию 'page'.

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

`WP_Post|array|null` — WP_Post

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

Файл: `wp-includes/post.php:6151`

```php
function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
	global $wpdb;

	$last_changed = wp_cache_get_last_changed( 'posts' );

	$hash      = md5( $page_path . serialize( $post_type ) );
	$cache_key = "get_page_by_path:$hash";
	$cached    = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed );
	if ( false !== $cached ) {
		// Special case: '0' is a bad `$page_path`.
		if ( '0' === $cached || 0 === $cached ) {
			return null;
		} else {
			return get_post( $cached, $output );
		}
	}

	$page_path     = rawurlencode( urldecode( $page_path ) );
	$page_path     = str_replace( '%2F', '/', $page_path );
	$page_path     = str_replace( '%20', ' ', $page_path );
	$parts         = explode( '/', trim( $page_path, '/' ) );
	$parts         = array_map( 'sanitize_title_for_query', $parts );
	$escaped_parts = esc_sql( $parts );

	$in_string = "'" . implode( "','", $escaped_parts ) . "'";

	if ( is_array( $post_type ) ) {
		$post_types = $post_type;
	} else {
		$post_types = array( $post_type, 'attachment' );
	}

	$post_types          = esc_sql( $post_types );
	$post_type_in_string = "'" . implode( "','", $post_types ) . "'";
	$sql                 = "
		SELECT ID, post_name, post_parent, post_type
		FROM $wpdb->posts
		WHERE post_name IN ($in_string)
		AND post_type IN ($post_type_in_string)
	";

	$pages = $wpdb->get_results( $sql, OBJECT_K );

	$revparts = array_reverse( $parts );

	$found_id = 0;
	foreach ( (array) $pages as $page ) {
		if ( $page->post_name === $revparts[0] ) {
			$count = 0;
			$p     = $page;

			/*
			 * Loop through the given path parts from right to left,
			 * ensuring each matches the post ancestry.
			 */
			while ( 0 !== (int) $p->post_parent && isset( $pages[ $p->post_parent ] ) ) {
				++$count;
				$parent = $pages[ $p->post_parent ];
				if ( ! isset( $revparts[ $count ] ) || $parent->post_name !== $revparts[ $count ] ) {
					break;
				}
				$p = $parent;
			}

			if ( 0 === (int) $p->post_parent
				&& count( $revparts ) === $count + 1
				&& $p->post_name === $revparts[ $count ]
			) {
				$found_id = $page->ID;
				if ( $page->post_type === $post_type ) {
					break;
				}
			}
		}
	}

	// We cache misses as well as hits.
	wp_cache_set_salted( $cache_key, $found_id, 'post-queries', $last_changed );

	if ( $found_id ) {
		return get_post( $found_id, $output );
	}

	return null;
}
```

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

- 2.1.0 — Introduced.

## Связанные

Использует: [`wp_cache_get_salted`](https://chugunov.pro/api-wordpress/functions/wp_cache_get_salted/), [`wp_cache_set_salted`](https://chugunov.pro/api-wordpress/functions/wp_cache_set_salted/), [`wp_cache_get_last_changed`](https://chugunov.pro/api-wordpress/functions/wp_cache_get_last_changed/), [`esc_sql`](https://chugunov.pro/api-wordpress/functions/esc_sql/), [`get_post`](https://chugunov.pro/api-wordpress/functions/get_post/), `wpdb::get_results`.
Используется в: [`get_post_embed_url`](https://chugunov.pro/api-wordpress/functions/get_post_embed_url/), [`wp_resolve_numeric_slug_conflicts`](https://chugunov.pro/api-wordpress/functions/wp_resolve_numeric_slug_conflicts/), [`wp_install_maybe_enable_pretty_permalinks`](https://chugunov.pro/api-wordpress/functions/wp_install_maybe_enable_pretty_permalinks/), `WP::parse_request`, `WP_Query::is_page`, `WP_Query::is_single`, `WP_Query::get_posts`, `WP_Query::parse_query`, [`url_to_postid`](https://chugunov.pro/api-wordpress/functions/url_to_postid/).

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