On this page
get_page_by_path( string $page_path, string $output = OBJECT, string|array $post_type = 'page' ): WP_Post|array|null
Retrieves a page given its path.
Parameters
$page_pathstring Required-
Page path.
$outputstring Optional-
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 $post_typestring|array Optional-
Post type or array of post types. Default
'page'.Default:
'page'
Return
WP_Post|array|null WP_Post (or array) on success, or null on failure.
Source
File: wp-includes/post.php. View all references
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:$last_changed";
$cached = wp_cache_get( $cache_key, 'posts' );
if ( false !== $cached ) {
// Special case: '0' is a bad `$page_path`.
if ( '0' === $cached || 0 === $cached ) {
return;
} 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 );
$foundid = 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 != $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 == $p->post_parent && count( $revparts ) == $count + 1 && $p->post_name == $revparts[ $count ] ) {
$foundid = $page->ID;
if ( $page->post_type == $post_type ) {
break;
}
}
}
}
// We cache misses as well as hits.
wp_cache_set( $cache_key, $foundid, 'posts' );
if ( $foundid ) {
return get_post( $foundid, $output );
}
return null;
}
Related
Uses
| Uses | Description |
|---|---|
| wp_cache_get_last_changed() wp-includes/functions.php | Gets last changed date for the specified cache group. |
| wp_cache_set() wp-includes/cache.php | Saves the data to the cache. |
| esc_sql() wp-includes/formatting.php | Escapes data for use in a MySQL query. |
| wp_cache_get() wp-includes/cache.php | Retrieves the cache contents from the cache by key and group. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
| wpdb::get_results() wp-includes/class-wpdb.php | Retrieves an entire SQL result set from the database (i.e., many rows). |
Used By
| Used By | Description |
|---|---|
| get_post_embed_url() wp-includes/embed.php | Retrieves the URL to embed a specific post in an iframe. |
| wp_resolve_numeric_slug_conflicts() wp-includes/rewrite.php | Resolves numeric slugs that collide with date permalinks. |
| wp_install_maybe_enable_pretty_permalinks() wp-admin/includes/upgrade.php | Maybe enable pretty permalinks on installation. |
| WP::parse_request() wp-includes/class-wp.php | Parses the request to find the correct WordPress query. |
| WP_Query::is_page() wp-includes/class-wp-query.php | Is the query for an existing single page? |
| WP_Query::is_single() wp-includes/class-wp-query.php | Is the query for an existing single post? |
| WP_Query::get_posts() wp-includes/class-wp-query.php | Retrieves an array of posts based on query variables. |
| WP_Query::parse_query() wp-includes/class-wp-query.php | Parse a query string and set query type booleans. |
| url_to_postid() wp-includes/rewrite.php | Examines a URL and try to determine the post ID it represents. |
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_page_by_path