wppaste
WordPress

wp_get_nav_menu_items( int|string|WP_Term $menu, array $args = array() ): array|false

Since
3.0.0
Source
wp-includes/nav-menu.php:712
Retrieves all menu items of a navigation menu.

Description

Note: Most arguments passed to the $args parameter – save for 'output_key' – are specifically for retrieving nav_menu_item posts from get_posts() and may only indirectly affect the ultimate ordering and content of the resulting nav menu items that get returned from this function.

Compatibility

WordPress
since 3.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$menuint|string|WP_Term
Menu ID, slug, name, or object.
$argsarrayoptional
Arguments to pass to get_posts().Default: array()
  • $orderstringdefault: 'ASC'

    How to order nav menu items as queried with get_posts(). Will be ignored if 'output' is ARRAY_A.
  • $orderbystringdefault: 'menu_order'

    Field to order menu items by as retrieved from get_posts(). Supply an orderby field via 'output_key' to affect the output order of nav menu items.
  • $post_typestringdefault: 'nav_menu_item'

    Menu items post type.
  • $post_statusstringdefault: 'publish'

    Menu items post status.
  • $outputstringdefault: ARRAY_A

    How to order outputted menu items.
  • $output_keystringdefault: 'menu_order'

    Key to use for ordering the actual menu items that get returned. Note that that is not a get_posts() argument and will only affect output of menu items processed in this function.
  • $nopagingbooldefault: true

    Whether to retrieve all menu items (true) or paginate (false).
  • $update_menu_item_cachebooldefault: true

    Whether to update the menu item cache.

Return value

array|false
Array of menu items, otherwise false.

Performance profile

How much work a call to wp_get_nav_menu_items() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_posts().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
8–75

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 85.

Plugin surface
1 hook

Third-party callbacks on 'wp_get_nav_menu_items' run inside this call, and their cost is not bounded by anything here.

Called by
9

9 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_posts()called directly
  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 10 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_nav_menu_items() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8wp_get_nav_menu_object()
!taxonomy_exists()12wp_get_nav_menu_object(), taxonomy_exists()
taxonomy_exists() && is_admin()55wp_get_nav_menu_object(), taxonomy_exists(), wp_parse_args(), array_map(), is_admin(), apply_filters()
taxonomy_exists() && is_admin()59wp_get_nav_menu_object(), taxonomy_exists(), wp_parse_args(), get_posts(), array_map(), is_admin(), apply_filters()
taxonomy_exists() && !is_admin()60wp_get_nav_menu_object(), taxonomy_exists(), wp_parse_args(), array_map(), is_admin(), array_filter(), apply_filters()
taxonomy_exists() && !is_admin()64wp_get_nav_menu_object(), taxonomy_exists(), wp_parse_args(), get_posts(), array_map(), is_admin(), array_filter(), apply_filters()
taxonomy_exists() && is_admin()65–66wp_get_nav_menu_object(), taxonomy_exists(), wp_parse_args(), array_map(), is_admin(), ASC(), apply_filters()
taxonomy_exists() && is_admin()69–70wp_get_nav_menu_object(), taxonomy_exists(), wp_parse_args(), get_posts(), array_map(), is_admin(), ASC(), apply_filters()
taxonomy_exists() && !is_admin()70–71wp_get_nav_menu_object(), taxonomy_exists(), wp_parse_args(), array_map(), is_admin(), array_filter(), ASC(), apply_filters()
taxonomy_exists() && !is_admin()74–75wp_get_nav_menu_object(), taxonomy_exists(), wp_parse_args(), get_posts(), array_map(), is_admin(), array_filter(), ASC(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 85 instructions, 8–75 executed per call, 7 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_get_nav_menu_items() runs, in this order:

  1. apply_filters( wp_get_nav_menu_items )filterline 777 (+65 into the body)

    Filters the navigation menu items being returned.

Uses · 7

  • wp_get_nav_menu_object()Returns a navigation menu object.
  • taxonomy_exists()Determines whether the taxonomy name exists.
  • wp_parse_args()Merges user defined arguments into defaults array.
  • get_posts()Retrieves an array of the latest posts, or posts matching the given criteria.
  • is_admin()Determines whether the current request is for an administrative interface page.
  • wp_list_sort()Sorts an array of objects or arrays based on one or more orderby arguments.
  • apply_filters()Calls the callback functions that have been added to a filter hook.

Used by · 9

Source code

function wp_get_nav_menu_items( $menu, $args = array() ) {	$menu = wp_get_nav_menu_object( $menu ); 	if ( ! $menu ) {		return false;	} 	if ( ! taxonomy_exists( 'nav_menu' ) ) {		return false;	} 	$defaults = array(		'order'                  => 'ASC',		'orderby'                => 'menu_order',		'post_type'              => 'nav_menu_item',		'post_status'            => 'publish',		'output'                 => ARRAY_A,		'output_key'             => 'menu_order',		'nopaging'               => true,		'update_menu_item_cache' => true,		'tax_query'              => array(			array(				'taxonomy' => 'nav_menu',				'field'    => 'term_taxonomy_id',				'terms'    => $menu->term_taxonomy_id,			),		),	);	$args     = wp_parse_args( $args, $defaults );	if ( $menu->count > 0 ) {		$items = get_posts( $args );	} else {		$items = array();	} 	$items = array_map( 'wp_setup_nav_menu_item', $items ); 	if ( ! is_admin() ) { // Remove invalid items only on front end.		$items = array_filter( $items, '_is_valid_nav_menu_item' );	} 	if ( ARRAY_A === $args['output'] ) {		$items = wp_list_sort(			$items,			array(				$args['output_key'] => 'ASC',			)		); 		$i = 1; 		foreach ( $items as $k => $item ) {			$items[ $k ]->{$args['output_key']} = $i++;		}	} 	/**	 * Filters the navigation menu items being returned.	 *	 * @since 3.0.0	 *	 * @param array  $items An array of menu item post objects.	 * @param object $menu  The menu object.	 * @param array  $args  An array of arguments used to retrieve menu item objects.	 */	return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );}

Changelog

Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/nav-menu.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.