WordPress.org

Make WordPress Core

Ticket #3373: get_page_uri.patch

File get_page_uri.patch, 1.3 KB (added by ryanscheuermann, 12 years ago)

fixed bug: include attachment uris

  • wp-includes/post.php

     
    10251025        return $result;
    10261026}
    10271027
     1028function _get_page_uri_hierarchy($posts, $parent = 0, $parent_uri = '') {
     1029        $result = array ( );
     1030        if ($posts) { foreach ($posts as $post) {
     1031                if ($post->post_parent == $parent) {
     1032
     1033                        $current_uri = urldecode($post->post_name);
     1034                        if($parent_uri != '')
     1035                                $current_uri = $parent_uri . '/'. $current_uri;
     1036                        $result[$post->ID] = $current_uri;
     1037                        $children = _get_page_uri_hierarchy($posts, $post->ID, $current_uri);
     1038                        $result += $children; //append $children to $result
     1039                }
     1040        } }
     1041        return $result;
     1042}
     1043
    10281044function get_page_uri($page_id) {
    1029         $page = get_page($page_id);
    1030         $uri = urldecode($page->post_name);
     1045        global $uris, $wpdb;
    10311046
    1032         // A page cannot be it's own parent.
    1033         if ( $page->post_parent == $page->ID )
    1034                 return $uri;
     1047        if(!is_array($uris))
     1048                $uris = _get_page_uri_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page' OR post_type='attachment'"));
    10351049
    1036         while ($page->post_parent != 0) {
    1037                 $page = get_page($page->post_parent);
    1038                 $uri = urldecode($page->post_name) . "/" . $uri;
    1039         }
    1040 
    1041         return $uri;
     1050        return $uris[$page_id];
    10421051}
    10431052
    10441053function &get_pages($args = '') {