get_page_uri()WP 1.5.0

Build the URI path for a page.

Sub pages will be in the "directory" under the parent page post name.

1 time — 0.000237 sec (fast) | 50000 times — 3.85 sec (fast)
Hooks from the function

Return

String|false. Page URI, false on error.

Usage

get_page_uri( $page );
$page(WP_Post|object|int)
Page ID or WP_Post object.
Default: global $post

Examples

0

#1 Get the path to the current page

Suppose we have a page 1544 with label mypage and it is a child of parent. Then:

echo get_page_uri( 1544 ); // return: parent/mypage

Changelog

Since 1.5.0 Introduced.
Since 4.6.0 The $page parameter was made optional.

get_page_uri() code WP 6.4.3

function get_page_uri( $page = 0 ) {
	if ( ! $page instanceof WP_Post ) {
		$page = get_post( $page );
	}

	if ( ! $page ) {
		return false;
	}

	$uri = $page->post_name;

	foreach ( $page->ancestors as $parent ) {
		$parent = get_post( $parent );
		if ( $parent && $parent->post_name ) {
			$uri = $parent->post_name . '/' . $uri;
		}
	}

	/**
	 * Filters the URI for a page.
	 *
	 * @since 4.4.0
	 *
	 * @param string  $uri  Page URI.
	 * @param WP_Post $page Page object.
	 */
	return apply_filters( 'get_page_uri', $uri, $page );
}