WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a menu name Social Network. I want to get the menus ID. I tried the following, but didn't succeed.

  global $wpdb;
$menu_slug = 'social-network';
$menu_id = $wpdb->get_results(
    FROM $wpdb->wp_terms
    WHERE name = ".$menu_slug."
echo $menu_id;
share|improve this question
What exactly is inside $args? – kaiser Jun 26 '13 at 9:38

You can use the function get_term_by and use 'name' in the field param.

<?php get_term_by( $field, $value, $taxonomy, $output, $filter ) ?> 


$term = get_term_by('name', 'Social Network', 'nav_menu');
$menu_id = $term->term_id;

Here is the link to the codex page: http://codex.wordpress.org/Function_Reference/get_term_by

Hope this helps.

share|improve this answer

All you need is get_terms()

Let's write wp_menu_id_by_name( $name )

 * Gets a menu id by name
 * @param string $name The menu name
 * @return int|boolean The menu id or false if not found
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;

Let's use it

echo 'My Special Menu id is ' . wp_menu_id_by_name( 'My Special menu' );

// Outputs: My Special Menu id is 3
share|improve this answer

You don't use the right code, use this instead:

global $wpdb;

$tablename = $wpdb->prefix.'terms'; // use always table prefix
$menu_name = 'top-menu'; // menu name
$menu_id = $wpdb->get_results(
    SELECT term_id
    FROM ".$tablename." 
    WHERE name= '".$menu_name."'

// results in array 
foreach($menu_id as $menu):
    echo $menu->term_id;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.