Make WordPress Core


Ignore:
Timestamp:
02/10/2006 12:54:16 AM (21 years ago)
Author:
ryan
Message:

get_page_by_path. Some page uri cleanups.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r3510 r3511  
    664664
    665665        return $page;
     666}
     667
     668function get_page_by_path($page_path) {
     669        global $wpdb;
     670
     671        $page_path = str_replace('%2F', '/', urlencode(urldecode($page_path)));
     672        $page_paths = '/' . trim($page_path, '/');
     673        $leaf_path  = sanitize_title(basename($page_paths));
     674        $page_paths = explode('/', $page_paths);
     675        foreach($page_paths as $pathdir)
     676                $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
     677
     678        $pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = '$leaf_path'");
     679
     680        if ( empty($pages) )
     681                return 0;
     682
     683        foreach ($pages as $page) {
     684                $path = '/' . $leaf_path;
     685                $curpage = $page;
     686                while ($curpage->post_parent != 0) {
     687                        $curpage = $wpdb->get_row("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = '$curpage->post_parent'");
     688                        $path = '/' . $curpage->post_name . $path;
     689                }
     690
     691                if ( $path == $full_path )
     692                        return $page->ID;
     693        }
     694
     695        return 0;
    666696}
    667697
Note: See TracChangeset for help on using the changeset viewer.