Definition:
function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {}
Retrieves a page given its path.
Parameters
- string $page_path: Page path
- string $output: Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
- string $post_type: Optional. Post type. Default page.
Return values
returns:Null when complete.
Source code
function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') { global $wpdb; $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( 'esc_sql', $parts ); $parts = array_map( 'sanitize_title_for_query', $parts ); $in_string = "'". implode( "','", $parts ) . "'"; $post_type_sql = $post_type; $wpdb->escape_by_ref( $post_type_sql ); $pages = $wpdb->get_results( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name IN ($in_string) AND (post_type = '$post_type_sql' OR post_type = 'attachment')", OBJECT_K ); $revparts = array_reverse( $parts ); $foundid = 0; foreach ( (array) $pages as $page ) { if ( $page->post_name == $revparts[0] ) { $count = 0; $p = $page; while ( $p->post_parent != 0 && isset( $pages[ $p->post_parent ] ) ) { $count++; $parent = $pages[ $p->post_parent ]; if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) break; $p = $parent; } if ( $p->post_parent == 0 && $count+1 == count( $revparts ) && $p->post_name == $revparts[ $count ] ) { $foundid = $page->ID; break; } } } if ( $foundid ) return get_page( $foundid, $output ); return null; }
1530
No comments yet... Be the first to leave a reply!