get_page_by_path()

You are here:

get_page_by_path( string $page_pathstring $output = OBJECTstring|array $post_type = ‘page’ )

Retrieves a page given its path.

Parameters Parameters

$page_path

(string) (Required) Page path.

$output

(string) (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 value: OBJECT

$post_type

(string|array) (Optional) Post type or array of post types.

Default value: ‘page’


Top ↑

Return Return

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


Top ↑

Source Source

File: wp-includes/post.php

5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
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 );
    }
}


Top ↑

Changelog Changelog

Changelog
VersionDescription
2.1.0Introduced.
Was this article helpful?
Dislike 0
Views: 2

Cart

Log in

Create an Account
Back to Top