Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




get_page_by_path [ WordPress Function ]

get_page_by_path ( $page_path, $output = OBJECT, $post_type = 'page' )
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.
Uses:
  • $wpdb
Returns:
  • (mixed) Null when complete.
Defined at:



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 != && 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 == && $count+== 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

More ...

User discussions [ wordpress.org ]

0 User Note(s)

None yet. Be the first!

Add New ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics