Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

WP Nav Menu Structure

By: a guest on Apr 12th, 2012  |  syntax: PHP  |  size: 2.77 KB  |  views: 78  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.     static function get_nav_menu_struct($id) {
  2.    
  3.         global $wpdb;
  4.    
  5.         //We've confirmed this is our special site-map menu,
  6.         alert('jack-links presents: messin with sitemap');
  7.         $dat = array();
  8.    
  9.         //Get all object id's associated with this "site-map" nav menu
  10.         $nav_ids = $wpdb->get_col($wpdb->prepare("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $id ));
  11.    
  12.         //Get all nav_menu_item objects for the above nav_ids
  13.         $mitems = get_posts(array(
  14.             'post_type' => 'nav_menu_item',
  15.             'include' => $nav_ids,
  16.             'orderby' => 'menu_order',
  17.             'order' => 'ASC'
  18.         ));
  19.    
  20.         //Restructure by item ID and only keep the important keys
  21.         foreach ($mitems as $itm) {
  22.             $dat[$itm->ID] = array(
  23.                 'menu_order' => $itm->menu_order,
  24.                 'post_parent' => $itm->post_parent,
  25.                 'description' => trim($itm->post_content)
  26.             );
  27.         }
  28.    
  29.         //Query for extended nav item meta info which contains the target page ID and the parent menu item
  30.         $meta_info = $wpdb->get_results(
  31.             "SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta
  32.             WHERE  (meta_key = '_menu_item_object_id' OR meta_key = '_menu_item_menu_item_parent' OR meta_key = '_menu_item_classes') AND
  33.                    post_id in (" . join(",", $nav_ids) . ")");
  34.    
  35.         $post_ids = array();
  36.    
  37.         //merge the resulting meta info into the original dat array
  38.         foreach ($meta_info as $info) {
  39.             if ($info->meta_key == "_menu_item_object_id") {
  40.                 $post_ids[] = $dat[$info->post_id]['post_id'] = $info->meta_value;
  41.             }
  42.             else if ($info->meta_key == "_menu_item_menu_item_parent") {
  43.                 $dat[$info->post_id]['menu_parent'] = $info->meta_value;
  44.             }
  45.             else if ($info->meta_key == "_menu_item_classes") {
  46.                 $dat[$info->post_id]['classes'] = maybe_unserialize($info->meta_value);
  47.             }
  48.         }
  49.    
  50.         //Now we get the post title for easier debugging and post menu_order to sync sorting as well as hierarchy
  51.         $post_h = $wpdb->get_results("SELECT ID, post_title, menu_order FROM $wpdb->posts WHERE ID in (" . join(",",$post_ids) . ")");
  52.         $post_titles = array();
  53.         $post_orders = array();
  54.         foreach ($post_h as $p) {
  55.             $post_titles[$p->ID] = $p->post_title;
  56.             $post_orders[$p->ID] = $p->menu_order;
  57.         }
  58.         foreach ($dat as $k => $data) {
  59.             $dat[$k]['post_title'] = $post_titles[$data['post_id']];
  60.             $dat[$k]['post_order'] = $post_orders[$data['post_id']];
  61.         }
  62.    
  63.         return $dat;
  64.     }