WordPress.org

Make WordPress Core

Ticket #3390: get_page_uri.2.0.5

File get_page_uri.2.0.5, 1.4 KB (added by ryanscheuermann, 7 years ago)

better & faster get_page_uri

Line 
1Index: wp-includes/functions.php
2===================================================================
3--- wp-includes/functions.php   (revision 4497)
4+++ wp-includes/functions.php   (working copy)
5@@ -1340,20 +1340,29 @@
6        remove_filter($tag, $function_to_remove, $priority, $accepted_args);
7 }
8 
9+function _get_page_uri_hierarchy($posts, $parent = 0, $parent_uri = '') {
10+       $result = array ( );
11+       if ($posts) { foreach ($posts as $post) {
12+               if ($post->post_parent == $parent) {
13+
14+                       $current_uri = urldecode($post->post_name);
15+                       if($parent_uri != '')
16+                               $current_uri = $parent_uri . '/'. $current_uri;
17+                       $result[$post->ID] = $current_uri;
18+                       $children = _get_page_uri_hierarchy($posts, $post->ID, $current_uri);
19+                       $result += $children; //append $children to $result
20+               }
21+       } }
22+       return $result;
23+}
24+
25 function get_page_uri($page_id) {
26-       $page = get_page($page_id);
27-       $uri = urldecode($page->post_name);
28+       global $uris, $wpdb;
29 
30-       // A page cannot be it's own parent.
31-       if ( $page->post_parent == $page->ID )
32-               return $uri;
33+       if(!is_array($uris))
34+               $uris = _get_page_uri_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_status = 'static' OR post_status='attachment'"));
35 
36-       while ($page->post_parent != 0) {
37-               $page = get_page($page->post_parent);
38-               $uri = urldecode($page->post_name) . "/" . $uri;
39-       }
40-
41-       return $uri;
42+       return $uris[$page_id];
43 }
44 
45 function get_posts($args) {