How to get all registered menus in WordPress

It’s been a long time since last time I wrote about technical thing on this blog! But now I come back with a small tip of getting all registered menus in WordPress. Just a note: get all registered menus, not menu locations (for menu locations, WordPress already has a built in function – get_nav_menu_locations()). This is quite useful in some cases when you want to assign a custom menu for a page (like for onepage template – similar to what I did for our music and band WordPress theme).

WordPress get registered menus
Get it done in simple way!

The code snippet is very simple as the following:

$menus = get_terms( 'nav_menu' );
$menus = array_combine( wp_list_pluck( $menus, 'term_id' ), wp_list_pluck( $menus, 'name' ) );

print_r( $menus );

// You'll get:
// array( 'menu_id' => 'Menu Name' );

There are one interesting in the code above: each menu is a term of a taxonomy nav_menu. So it will have a full list of attributes that a normal term has, like: term_id, slug, name, etc. In this code, I only get term_id and name and combine them in an associated array, and that’s enough for use.

3 thoughts on “How to get all registered menus in WordPress

  1. Each menu is a custom post type, so if you know how to query a post type, you should be able to get the menus. Anyways I would recomend to check the database and see its structure. There most of the questions have the answers. 😉

    1. Precisely, each menu item is a post of a CPT, not the menu itself. Menu is a term of a taxonomy nav_menu.

  2. I never really had a problem with the menu on my blog but I’m sure many have. Great post nonetheless.

Comments are closed.

Newsletter

Subscribe to our newsletter to receive news and tutorials about WordPress and web development.

Want to get away from Pipdig drama? See 30+ Pipdig blog themes alternatives See here →