wp_get_nav_menu_object( int|string|WP_Term $menu ): WP_Term|false

Returns a navigation menu object.

Parameters

$menuint|string|WP_Termrequired
Menu ID, slug, name, or object.

Return

WP_Term|false Menu object on success, false if $menu param isn’t supplied or term does not exist.

Source

function wp_get_nav_menu_object( $menu ) {
	$menu_obj = false;

	if ( is_object( $menu ) ) {
		$menu_obj = $menu;
	}

	if ( $menu && ! $menu_obj ) {
		$menu_obj = get_term( $menu, 'nav_menu' );

		if ( ! $menu_obj ) {
			$menu_obj = get_term_by( 'slug', $menu, 'nav_menu' );
		}

		if ( ! $menu_obj ) {
			$menu_obj = get_term_by( 'name', $menu, 'nav_menu' );
		}
	}

	if ( ! $menu_obj || is_wp_error( $menu_obj ) ) {
		$menu_obj = false;
	}

	/**
	 * Filters the nav_menu term retrieved for wp_get_nav_menu_object().
	 *
	 * @since 4.3.0
	 *
	 * @param WP_Term|false      $menu_obj Term from nav_menu taxonomy, or false if nothing had been found.
	 * @param int|string|WP_Term $menu     The menu ID, slug, name, or object passed to wp_get_nav_menu_object().
	 */
	return apply_filters( 'wp_get_nav_menu_object', $menu_obj, $menu );
}

Hooks

apply_filters( ‘wp_get_nav_menu_object’, WP_Term|false $menu_obj, int|string|WP_Term $menu )

Filters the nav_menu term retrieved for wp_get_nav_menu_object() .

Changelog

VersionDescription
3.0.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Examples

    // By name.
    $menu = wp_get_nav_menu_object( 'wpdocs mainmenu' );
    
    // By slug.
    $menu = wp_get_nav_menu_object( 'wpdocs-mainmenu' );
    
    // By ID.
    $menu_name = 'wpdocs mainmenu';
    $menu_obj  = get_term_by( 'name', $menu_name, 'nav_menu' );
    $menu_id   = $menu_obj->term_id;
    $menu = wp_get_nav_menu_object( $menu_id );
    
    
    // By location.
    $menu_name = 'primary';
    $locations = get_nav_menu_locations();
    $menu_id   = $locations[ $menu_name ] ;
    wp_get_nav_menu_object( $menu_id );

You must log in before being able to contribute a note or feedback.