WordPress keeps the identification of all records by its ID. Similarly, all menu has their unique ID stored in the database. In order to perform functions on a menu, its ID is required to fetch it. WordPress allows us to fetch menu ID by menu name. In order to create or access a menu, navigate to appearance > menu in the admin panel:

In this article, we will see 3 different methods to fetch menu ID by menu name.
We have created a menu “My Menu” and used it as a reference:

Method 1: Built-in function
In the first method, we will use WordPress’s built-in function get_term_by.
The get_term_by() is a WordPress built-in function. This function is used to get the term data from the database from its term field and its data.
Let’s see an example below:
<?php // use get_term_by function $term = get_term_by('name', 'My Menu', 'nav_menu'); // get menu id $menu_id = $term->term_id; ?>
The variable $menu_id
stores the menu ID.
Method 2: SQL method
In the second method, we will write an SQL query that will help to get the menu ID from the database.
First of all, we will write the menu name. In this example we have used “My Menu”:
<?php $menu_name = 'My Menu'; ?>
Please note that you should write the exact menu name. “My Menu” is different from “My menu” and “my menu”. It is case-sensitive and should be exactly the same as defined in the WordPress admin panel.
Now we will be creating the table name along with the prefix. WordPress usually stores menu ID in its “terms” table. If you know your table prefix then you can use the prefix along with the “terms” and if you don’t know the prefix then not to worry at all you can use $wpdb->prefix
to get the table prefix.
<?php $table_name = $wpdb->prefix.'terms'; ?>
The $table_name
variable now has the table name along with the prefix and it will be used to create SQL query.
<?php $menu_id = $wpdb->get_results(" SELECT term_id FROM ".$table_name." WHERE name= '".$menu_name."' "); ?>
The $menu_id
will get the data received by the execution of the SQL query.
Now we will be fetching the menu ID as below:
<?php foreach($menu_id as $menu): echo $menu->term_id; endforeach; ?>
The $menu->term_id
will return the menu ID, now you can use that menu ID to fetch further details and objects for the particular menu.
The full PHP script:
<?php global $wpdb; // menu name $menu_name = 'My Menu'; // table name $table_name = $wpdb->prefix.'terms'; // SQL to get menu by menu name $menu_id = $wpdb->get_results(" SELECT term_id FROM ".$table_name." WHERE name= '".$menu_name."' "); // extract menu ID foreach($menu_id as $menu): echo $menu->term_id; endforeach; ?>
Method 3: Custom function
Here comes the third method. We will create a custom function that will take the menu name as a parameter and will return its ID.
<?php // define function to get menu ID by name function wp_menu_id_by_name( $name ) { $menus = get_terms( 'nav_menu' ); foreach ( $menus as $menu ) { if( $name === $menu->name ) { return $menu->term_id; } } return false; } // use the function echo 'The Menu id is ' . wp_menu_id_by_name( 'My Menu' ); ?>
There can be many other methods to fetch the menu ID, we only covered the 3 effective methods.