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:6151
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
- Scaling
- Scales with input
- Instructions
- 28–144
- Plugin surface
- None
- Called by
- 9
Reaches the database via ->get_results().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 158.
Nothing here hands control to plugin code.
9 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_get_last_changed()called directly - querycontent query
get_post()called directly - serializeserialisation
serialize()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.
| When | Instructions | Calls it makes |
|---|---|---|
$cached !== false | 28–30 | wp_cache_get_last_changed(), serialize(), md5(), wp_cache_get_salted() |
$cached !== false && $cached !== "0" && $cached !== 0 | 34 | wp_cache_get_last_changed(), serialize(), md5(), wp_cache_get_salted(), get_post() |
$cached === false | 97–140 | wp_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 === false | 101–144 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 158 | 28–144 | 16 | |
| 8.5 | 158 | 28–144 | 16 | |
| 8.4 | 158 | 28–144 | 16 | 15 fewer instructions than PHP 8.3 |
| 8.3 | 173 | 28–159 | 16 | |
| 8.2 | 173 | 28–159 | 16 | |
| 8.1 | 173 | 28–159 | 16 | |
| 7.4 | 173 | 28–159 | 16 |
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
- wp_cache_get_last_changed()Gets last changed date for the specified cache group.
- wp_cache_get_salted()Retrieves cached data if valid and unchanged.
- get_post()Retrieves post data given a post ID or post object.
- esc_sql()Escapes data for use in a MySQL query.
- wp_cache_set_salted()Stores salted data in the cache.
Used by · 9
- WP::parse_request()Parses the request to find the correct WordPress query.
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- WP_Query::is_page()Determines whether the query is for an existing single page.
- WP_Query::is_single()Determines whether the query is for an existing single post.
- WP_Query::parse_query()Parses a query string and sets query type booleans.
- get_post_embed_url()Retrieves the URL to embed a specific post in an iframe.
- url_to_postid()Examines a URL and try to determine the post ID it represents.
- wp_install_maybe_enable_pretty_permalinks()Maybe enable pretty permalinks on installation.
- wp_resolve_numeric_slug_conflicts()Resolves numeric slugs that collide with date permalinks.
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( $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 ) {Changelog
Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.