get_page_by_path [ WordPress Function ]
get_page_by_path ( $page_path, $output = OBJECT, $post_type = 'page' )
| Parameters: |
|
| Uses: |
|
| Returns: |
|
| Defined at: |
|
Similar Functions: get_page_by_title, get_category_by_path, get_page_template, get_paged_template, get_page_templates
Retrieves a page given its path.
Source
<?php
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;
}
?>
Examples [ wp-snippets.com ]
Top Google Search Results
- Function Reference/get page by path « WordPress Codex
Function Reference/get page by path. Contents. 1 Description; 2 Usage ... Usage. <?php get_page_by_path( $page_path, $output, $post_type ) ?> Parameters ...
codex.wordpress.org - what exactly does 'get_page_by_path' wordpress function do ...
Internally, get_page_by_path() eventually uses get_page() (source here), returning an object or array (depending on the second $output argument) ...
stackoverflow.com - permalinks - Get page link from slug? - WordPress
Dec 7, 2010 ... get_page_by_path() returns an array of all page information. ... It seems that get_page_by_path() can be quite complicated to use when ...
wordpress.stackexchange.com - get_page_by_path | A HitchHackers guide through WordPress
Feb 12, 2011 ... Source code. function get_page_by_path($page_path, $output = OBJECT, $ post_type = 'page') { global $wpdb; $page_path ...
hitchhackerguide.com