[wp-trac] Re: [WordPress Trac] #3566: Request for new function get_page_parents

WordPress Trac wp-trac at lists.automattic.com
Sun May 17 09:51:04 GMT 2009


#3566: Request for new function get_page_parents
-------------------------+--------------------------------------------------
 Reporter:  snakefoot    |       Owner:  anonymous            
     Type:  enhancement  |      Status:  new                  
 Priority:  normal       |   Milestone:  Future Release       
Component:  Template     |     Version:  2.1                  
 Severity:  normal       |    Keywords:  has-patch 2nd-opinion
-------------------------+--------------------------------------------------

Comment(by Denis-de-Bernardy):

 I use the following code in a few of my plugins, in case there is any
 interest:

 {{{
         function cache_pages() {
                 if ( is_page() ) {
                         global $wp_the_query;
                         $page_id = (int)
 $wp_the_query->get_queried_object_id();
                         $page = get_page($page_id);
                 } elseif ( get_option('show_on_front') == 'page' ) {
                         $page_id = (int) get_option('page_for_posts');
                         $page = get_page($page_id);
                 } else {
                         $page_id = 0;
                         $page = null;
                 }

                 $ancestors = wp_cache_get($page_id, 'page_ancestors');
                 if ( $ancestors === false ) {
                         $ancestors = array();
                         while ( $page && $page->post_parent != 0 ) {
                                 $ancestors[] = (int) $page->post_parent;
                                 $page = get_page($page->post_parent);
                         }
                         $ancestors = array_reverse($ancestors);
                         wp_cache_set($page_id, $ancestors,
 'page_ancestors');
                 }

                 $parent_ids = $ancestors;
                 array_unshift($parent_ids, 0);
                 if ( $page_id )
                         $parent_ids[] = $page_id;

                 $cached = true;
                 foreach ( $parent_ids as $parent_id ) {
                         $cached = is_array(wp_cache_get($parent_id,
 'page_children'));
                         if ( $cached === false )
                                 break;
                 }

                 if ( $cached )
                         return;

                 global $wpdb;

                 $roots = (array) $wpdb->get_col("
                         SELECT  posts.ID
                         FROM    $wpdb->posts as posts
                         WHERE   posts.post_type = 'page'
                         AND             posts.post_parent IN ( 0, $page_id
 )
                         ");

                 $parent_ids = array_merge($parent_ids, $roots,
 array($page_id));
                 $parent_ids = array_unique($parent_ids);
                 $parent_ids = array_map('intval', $parent_ids);

                 $pages = (array) $wpdb->get_results("
                         SELECT  posts.*
                         FROM    $wpdb->posts as posts
                         WHERE   posts.post_type = 'page'
                         AND             posts.post_status = 'publish'
                         AND             posts.post_parent IN ( " .
 implode(',', $parent_ids) . " )
                         ORDER BY posts.menu_order, posts.post_title
                         ");
                 update_post_cache($pages);

                 $children = array();
                 $to_cache = array();

                 foreach ( $parent_ids as $parent_id )
                         $children[$parent_id] = array();

                 foreach ( $pages as $page ) {
                         $children[$page->post_parent][] = $page->ID;
                         $to_cache[] = $page->ID;
                 }

                 update_postmeta_cache($to_cache);

                 $all_ancestors = array();

                 foreach ( $children as $parent => $child_ids ) {
                         foreach ( $child_ids as $key => $child_id ) {
                                 $all_ancestors[$child_id][] = $parent;
                                 if ( get_post_meta($child_id,
 '_widgets_exclude', true) )
                                         unset($child_ids[$key]);
                         }

                         wp_cache_set($parent, $child_ids,
 'page_children');
                 }

                 foreach ( $all_ancestors as $child_id => $parent_ids ) {
                         while ( $parent_ids[0] )
                                 $parent_ids =
 array_merge($all_ancestors[$parent_ids[0]], $parent_ids);
                         wp_cache_set($child_id, $parent_ids,
 'page_ancestors');
                 }
         } # cache_pages()
 }}}

 It's meant to cache parents and children of a static page, and all of its
 ancestors sibblings.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/3566#comment:15>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list