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..
EDIT:
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;
}