Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static function get_nav_menu_struct($id) {
- global $wpdb;
- //We've confirmed this is our special site-map menu,
- alert('jack-links presents: messin with sitemap');
- $dat = array();
- //Get all object id's associated with this "site-map" nav menu
- $nav_ids = $wpdb->get_col($wpdb->prepare("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $id ));
- //Get all nav_menu_item objects for the above nav_ids
- $mitems = get_posts(array(
- 'post_type' => 'nav_menu_item',
- 'include' => $nav_ids,
- 'orderby' => 'menu_order',
- 'order' => 'ASC'
- ));
- //Restructure by item ID and only keep the important keys
- foreach ($mitems as $itm) {
- $dat[$itm->ID] = array(
- 'menu_order' => $itm->menu_order,
- 'post_parent' => $itm->post_parent,
- 'description' => trim($itm->post_content)
- );
- }
- //Query for extended nav item meta info which contains the target page ID and the parent menu item
- $meta_info = $wpdb->get_results(
- "SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta
- WHERE (meta_key = '_menu_item_object_id' OR meta_key = '_menu_item_menu_item_parent' OR meta_key = '_menu_item_classes') AND
- post_id in (" . join(",", $nav_ids) . ")");
- $post_ids = array();
- //merge the resulting meta info into the original dat array
- foreach ($meta_info as $info) {
- if ($info->meta_key == "_menu_item_object_id") {
- $post_ids[] = $dat[$info->post_id]['post_id'] = $info->meta_value;
- }
- else if ($info->meta_key == "_menu_item_menu_item_parent") {
- $dat[$info->post_id]['menu_parent'] = $info->meta_value;
- }
- else if ($info->meta_key == "_menu_item_classes") {
- $dat[$info->post_id]['classes'] = maybe_unserialize($info->meta_value);
- }
- }
- //Now we get the post title for easier debugging and post menu_order to sync sorting as well as hierarchy
- $post_h = $wpdb->get_results("SELECT ID, post_title, menu_order FROM $wpdb->posts WHERE ID in (" . join(",",$post_ids) . ")");
- $post_titles = array();
- $post_orders = array();
- foreach ($post_h as $p) {
- $post_titles[$p->ID] = $p->post_title;
- $post_orders[$p->ID] = $p->menu_order;
- }
- foreach ($dat as $k => $data) {
- $dat[$k]['post_title'] = $post_titles[$data['post_id']];
- $dat[$k]['post_order'] = $post_orders[$data['post_id']];
- }
- return $dat;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement