On this page
wp_get_nav_menu_items( int|string|WP_Term $menu, array $args = array() ): array|false
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.
Parameters
$menuint|string|WP_Term Required-
Menu ID, slug, name, or object.
$argsarray Optional-
Arguments to pass to get_posts() .
orderstringHow to order nav menu items as queried with get_posts() . Will be ignored if'output'is ARRAY_A. Default'ASC'.orderbystringField 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.
Default'menu_order'.post_typestringMenu items post type. Default'nav_menu_item'.post_statusstringMenu items post status. Default'publish'.outputstringHow to order outputted menu items. Default ARRAY_A.output_keystringKey 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. Default'menu_order'.nopagingboolWhether to retrieve all menu items (true) or paginate (false). Default true.
More Arguments from get_posts( ... $args )
Array or string of Query parameters.
attachment_idintAttachment post ID. Used for'attachment'post_type.authorint|stringAuthor ID, or comma-separated list of IDs.author_namestringUser'user_nicename'.author__inint[]An array of author IDs to query from.author__not_inint[]An array of author IDs not to query from.cache_resultsboolWhether to cache post information. Default true.catint|stringCategory ID or comma-separated list of IDs (this or any children).category__andint[]An array of category IDs (AND in).category__inint[]An array of category IDs (OR in, no children).category__not_inint[]An array of category IDs (NOT in).category_namestringUse category slug (not name, this or any children).comment_countarray|intFilter results by comment count. Provide an integer to match comment count exactly. Provide an array with integer'value'and'compare'operator ('=','!=','>','>=','<','<=') to compare against comment_count in a specific way.comment_statusstringComment status.comments_per_pageintThe number of comments to return per page.
Default'comments_per_page'option.date_queryarraydayintDay of the month. Accepts numbers 1-31.exactboolWhether to search by exact keyword. Default false.fieldsstringPost fields to query for. Accepts:
''Returns an array of complete post objects (WP_Post[]).'ids'Returns an array of post IDs (int[]).'id=>parent'Returns an associative array of parent post IDs, keyed by post ID (int[]).
''.hourintHour of the day. Accepts numbers 0-23.ignore_sticky_postsint|boolWhether to ignore sticky posts or not. Setting this to false excludes stickies from'post__in'. Accepts1|true,0|false.
Default false.mintCombination YearMonth. Accepts any four-digit year and month numbers 01-12.meta_keystring|string[]Meta key or keys to filter by.meta_valuestring|string[]Meta value or values to filter by.meta_comparestringMySQL operator used for comparing the meta value.
See WP_Meta_Query::__construct() for accepted values and default value.meta_compare_keystringMySQL operator used for comparing the meta key.
See WP_Meta_Query::__construct() for accepted values and default value.meta_typestringMySQL data type that the meta_value column will be CAST to for comparisons.
See WP_Meta_Query::__construct() for accepted values and default value.meta_type_keystringMySQL data type that the meta_key column will be CAST to for comparisons.
See WP_Meta_Query::__construct() for accepted values and default value.meta_queryarrayAn associative array of WP_Meta_Query arguments.
See WP_Meta_Query::__construct() for accepted values.menu_orderintThe menu order of the posts.minuteintMinute of the hour. Accepts numbers 0-59.monthnumintThe two-digit month. Accepts numbers 1-12.namestringPost slug.nopagingboolShow all posts (true) or paginate (false). Default false.no_found_rowsboolWhether to skip counting the total rows found. Enabling can improve performance. Default false.offsetintThe number of posts to offset before retrieval.orderstringDesignates ascending or descending order of posts. Default'DESC'.
Accepts'ASC','DESC'.orderbystring|arraySort retrieved posts by parameter. One or more options may be passed.
To use'meta_value', or'meta_value_num','meta_key=keyname'must be also be defined. To sort by a specific$meta_queryclause, use that clause's array key. Accepts:
'none''name''author''date''title''modified''menu_order''parent''ID''rand''relevance''RAND(x)'(where'x'is an integer seed value)'comment_count''meta_value''meta_value_num''post__in''post_name__in''post_parent__in'- The array keys of
$meta_query.
'date', except when a search is being performed, when the default is'relevance'.pintPost ID.pageintShow the number of posts that would show up on page X of a static front page.pagedintThe number of the current page.page_idintPage ID.pagenamestringPage slug.permstringShow posts if user has the appropriate capability.ping_statusstringPing status.post__inint[]An array of post IDs to retrieve, sticky posts will be included.post__not_inint[]An array of post IDs not to retrieve. Note: a string of comma- separated IDs will NOT work.post_mime_typestringThe mime type of the post. Used for'attachment'post_type.post_name__instring[]An array of post slugs that results must match.post_parentintPage ID to retrieve child pages for. Use 0 to only retrieve top-level pages.post_parent__inint[]An array containing parent page IDs to query child pages from.post_parent__not_inint[]An array containing parent page IDs not to query child pages from.post_typestring|string[]A post type slug (string) or array of post type slugs.
Default'any'if using'tax_query'.post_statusstring|string[]A post status (string) or array of post statuses.posts_per_pageintThe number of posts to query for. Use -1 to request all posts.posts_per_archive_pageintThe number of posts to query for by archive page. Overrides'posts_per_page'when is_archive() , or is_search() are true.sstringSearch keyword(s). Prepending a term with a hyphen will exclude posts matching that term. Eg, 'pillow -sofa' will return posts containing'pillow'but not'sofa'. The character used for exclusion can be modified using the the'wp_query_search_exclusion_prefix'filter.secondintSecond of the minute. Accepts numbers 0-59.sentenceboolWhether to search by phrase. Default false.suppress_filtersboolWhether to suppress filters. Default false.tagstringTag slug. Comma-separated (either), Plus-separated (all).tag__andint[]An array of tag IDs (AND in).tag__inint[]An array of tag IDs (OR in).tag__not_inint[]An array of tag IDs (NOT in).tag_idintTag id or comma-separated list of IDs.tag_slug__andstring[]An array of tag slugs (AND in).tag_slug__instring[]An array of tag slugs (OR in). unless'ignore_sticky_posts'is true. Note: a string of comma-separated IDs will NOT work.tax_queryarraytitlestringPost title.update_post_meta_cacheboolWhether to update the post meta cache. Default true.update_post_term_cacheboolWhether to update the post term cache. Default true.update_menu_item_cacheboolWhether to update the menu item cache. Default false.lazy_load_term_metaboolWhether to lazy-load term meta. Setting to false will disable cache priming for term meta, so that each get_term_meta() call will hit the database.
Defaults to the value of$update_post_term_cache.wintThe week number of the year. Accepts numbers 0-53.yearintThe four-digit year. Accepts any four-digit year.
Default:
array()
Return
array|false Array of menu items, otherwise false.
Source
File: wp-includes/nav-menu.php. View all references
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 );
}
Hooks
- apply_filters( 'wp_get_nav_menu_items',
array $items ,object $menu ,array $args ) -
Filters the navigation menu items being returned.
Related
Uses
| Uses | Description |
|---|---|
| wp_list_sort() wp-includes/functions.php | Sorts an array of objects or arrays based on one or more orderby arguments. |
| taxonomy_exists() wp-includes/taxonomy.php | Determines whether the taxonomy name exists. |
| get_posts() wp-includes/post.php | Retrieves an array of the latest posts, or posts matching the given criteria. |
| wp_get_nav_menu_object() wp-includes/nav-menu.php | Returns a navigation menu object. |
| is_admin() wp-includes/load.php | Determines whether the current request is for an administrative interface page. |
| wp_parse_args() wp-includes/functions.php | Merges user defined arguments into defaults array. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| WP_Customize_Nav_Menus::customize_register() wp-includes/class-wp-customize-nav-menus.php | Adds the customizer settings and controls. |
| wp_nav_menu_update_menu_items() wp-admin/includes/nav-menu.php | Saves nav menu items |
| wp_get_nav_menu_to_edit() wp-admin/includes/nav-menu.php | Returns the menu formatted to edit. |
| wp_nav_menu() wp-includes/nav-menu-template.php | Displays a navigation menu. |
| _wp_auto_add_pages_to_menu() wp-includes/nav-menu.php | Automatically add newly published page objects to menus with that as an option. |
| wp_update_nav_menu_item() wp-includes/nav-menu.php | Saves the properties of a menu item or create a new one. |
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_get_nav_menu_items