I want to get the nav menu name by the current post ID. After hours searching in my database I found out that navigation menus are basically just terms of the taxonomy 'nav_menu'. But for some reason they don't work the same.

I have tried to get the terms (nav menu's) with the get_the_terms() function, but this does not return anything.

global $post;
get_the_terms( $post->ID, 'nav_menu' );

I cannot find another function to get the 'terms' of the 'nav_menu' taxonomy. wp_get_nav_menu_items for example, returns the posts in the menu, and that is not what I need.

I think I need to make my own function, but I cannot find how the database structure for navigation menus work..


I made the following function which works, But @Rars showed me a quicker and cleaner option.

function get_post_menu( $post_id = null, $output = 'term_id' ) {
    if( ! isset( $post_id ) ) {
        global $post;
        $post_id = $post->ID;   
    global $wpdb;
    $menu = $wpdb->get_results( "
        SELECT term_taxonomy_id 
        FROM $wpdb->term_relationships
        WHERE object_id = 
        (SELECT post_id 
        FROM $wpdb->postmeta 
        WHERE meta_key = '_menu_item_object_id' 
        AND meta_value = $post_id)"
        , OBJECT 
    $term = get_term_by( 'term_taxonomy_id', $menu[0]->term_taxonomy_id, 'nav_menu' );
    return $term->$output;

1 Answer 1


You are missing a link. The posts are not assigned to the menus directly. There is a post type nav_menu_item in nav_menu taxonomy which links posts (or other kinds of destinations) to their place in menu.

This snippet should get you started on retrieval:

$menu_items = get_posts( [
    'post_type'  => 'nav_menu_item',
    'meta_query' => [
            'key'   => '_menu_item_object_id',
            'value' => get_the_ID(),
] );

foreach ( $menu_items as $menu_item ) {
    var_dump( get_the_terms( $menu_item->ID, 'nav_menu' ) );
  • A made a function which works (watch my question), but I think your answer is a better one.
    – Robbert
    Jan 9, 2015 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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