wp_get_nav_menu_items

Return all menu items of a navigation menu.

Description

(false|array) wp_get_nav_menu_items( (string) $menu, (array) $args = array() ); 

Returns (false|array)

$items Array of menu items, otherwise false.

Parameters (2)

0. $menu (string)
Menu name, ID, or slug.
1. $args — Optional. (array) => array()
Arguments to pass to get_posts().

Usage

  1. if ( !function_exists( 'wp_get_nav_menu_items' ) ) { 
  2. require_once ABSPATH . WPINC . '/nav-menu.php'; 
  3.  
  4. // Menu name, ID, or slug. 
  5. $menu = ''; 
  6.  
  7. // Optional. Arguments to pass to get_posts(). 
  8. $args = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_get_nav_menu_items($menu, $args); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/nav-menu.php  
  1. function wp_get_nav_menu_items( $menu, $args = array() ) { 
  2. $menu = wp_get_nav_menu_object( $menu ); 
  3.  
  4. if ( ! $menu ) { 
  5. return false; 
  6.  
  7. static $fetched = array(); 
  8.  
  9. $items = get_objects_in_term( $menu->term_id, 'nav_menu' ); 
  10. if ( is_wp_error( $items ) ) { 
  11. return false; 
  12.  
  13. $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',  
  14. 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true ); 
  15. $args = wp_parse_args( $args, $defaults ); 
  16. $args['include'] = $items; 
  17.  
  18. if ( ! empty( $items ) ) { 
  19. $items = get_posts( $args ); 
  20. } else { 
  21. $items = array(); 
  22.  
  23. // Get all posts and terms at once to prime the caches 
  24. if ( empty( $fetched[$menu->term_id] ) || wp_using_ext_object_cache() ) { 
  25. $fetched[$menu->term_id] = true; 
  26. $posts = array(); 
  27. $terms = array(); 
  28. foreach ( $items as $item ) { 
  29. $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true ); 
  30. $object = get_post_meta( $item->ID, '_menu_item_object', true ); 
  31. $type = get_post_meta( $item->ID, '_menu_item_type', true ); 
  32.  
  33. if ( 'post_type' == $type ) 
  34. $posts[$object][] = $object_id; 
  35. elseif ( 'taxonomy' == $type) 
  36. $terms[$object][] = $object_id; 
  37.  
  38. if ( ! empty( $posts ) ) { 
  39. foreach ( array_keys($posts) as $post_type ) { 
  40. get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) ); 
  41. unset($posts); 
  42.  
  43. if ( ! empty( $terms ) ) { 
  44. foreach ( array_keys($terms) as $taxonomy ) { 
  45. get_terms( $taxonomy, array( 
  46. 'include' => $terms[ $taxonomy ],  
  47. 'hierarchical' => false,  
  48. ) ); 
  49. unset($terms); 
  50.  
  51. $items = array_map( 'wp_setup_nav_menu_item', $items ); 
  52.  
  53. if ( ! is_admin() ) { // Remove invalid items only in front end 
  54. $items = array_filter( $items, '_is_valid_nav_menu_item' ); 
  55.  
  56. if ( ARRAY_A == $args['output'] ) { 
  57. $items = wp_list_sort( $items, array( 
  58. $args['output_key'] => 'ASC',  
  59. ) ); 
  60. $i = 1; 
  61. foreach ( $items as $k => $item ) { 
  62. $items[$k]->{$args['output_key']} = $i++; 
  63.  
  64. /** 
  65. * Filters the navigation menu items being returned. 
  66. * @since 3.0.0 
  67. * @param array $items An array of menu item post objects. 
  68. * @param object $menu The menu object. 
  69. * @param array $args An array of arguments used to retrieve menu item objects. 
  70. */ 
  71. return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );