wppaste
WordPress

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

Since
2.1.0
Source
wp-includes/post.php:6273
Retrieves a page given its path.

Compatibility

WordPress
since 2.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$page_pathstring
Page path.
$outputstringoptional
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.Default: OBJECT
$post_typestring|arrayoptional
Post type or array of post types. Default 'page'.Default: 'page'

Return value

WP_Post|array|null
WP_Post (or array) on success, or null on failure.

Performance profile

How much work a call to get_page_by_path() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_results().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
28–145

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 160.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
9

9 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • cacheobject cachewp_cache_get_last_changed()called directly
  • querycontent queryget_post()called directly
  • serializeserialisationserialize()called directly
  • sqldatabase query->get_results()called directly

Further down the call graph this can also reach hook, option and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_page_by_path() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$cached !== false28–30wp_cache_get_last_changed(), serialize(), md5(), wp_cache_get_salted()
$cached !== false && $cached !== "0" && $cached !== 035wp_cache_get_last_changed(), serialize(), md5(), wp_cache_get_salted(), get_post()
$cached === false97–140wp_cache_get_last_changed(), serialize(), md5(), wp_cache_get_salted(), urldecode(), rawurlencode(), explode(), array_map(), esc_sql(), esc_sql(), ->get_results(), array_reverse(), wp_cache_set_salted()
$cached === false102–145wp_cache_get_last_changed(), serialize(), md5(), wp_cache_get_salted(), urldecode(), rawurlencode(), explode(), array_map(), esc_sql(), esc_sql(), ->get_results(), array_reverse(), wp_cache_set_salted(), get_post()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev16028–14516
8.516028–14516
8.416028–1451615 fewer instructions than PHP 8.3
8.317528–16016
8.217528–16016
8.117528–16016
7.417528–16016

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 5

Used by · 9

Source code

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( (int) $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)	"; 	/** @var array<object{ ID: string, post_name: string, post_parent: string, post_type: string }> $pages */	$pages = $wpdb->get_results( $sql, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- The escaping has been applied above via esc_sql(). 	$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 );

Changelog

Introduced in 2.1.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 7.1.0 tag, from src/wp-includes/post.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.