I'm trying to get the current-menu-item-id in any page. I have used this solution, that brings me very close to the solution.

The problem I have is, that the function below is going through all the menus on the page, and not only the one specific menu, that I want it to go through:

add_filter( 'wp_nav_menu_objects', 'wpse16243_wp_nav_menu_objects' );  
function wpse16243_wp_nav_menu_objects( $sorted_menu_items )  
    foreach ( $sorted_menu_items as $menu_item ) {  
        if ( $menu_item->current ) {  
            $GLOBALS['wpse16243_title'] = $menu_item->ID;  
    return $sorted_menu_items;  

...which means that if you link to the same page more than once in your menus, you can risk that this function returns the current-menu-item-id from the wrong menu.

Is there any way that I can limit this function to only go through a specific menu and not all menus? I tried to pass the specific menu items in the variable/parameter $sorted_menu_items, but that seems not to work.

  • 1
    if a page is present twice in the menu, the URL is the same for the 2 then it's not possible to know the difference.
    – mmm
    Commented Jul 19, 2016 at 9:20
  • @user3199063, without knowing your use case for the id, it's difficult to give an anser. If instead of asking about how to modify that function you tell us what you intend we might come with other solutions. Have you tried using the filter nav_menu_item_id?
    – Luis Sanz
    Commented Jul 19, 2016 at 9:41
  • @mmm, indeed WordPress has a function (_nav_menu_item_id_use_once) that prevents that the same id is used twice. The second time a menu item appears it will share the same link and classes, but it won't have an ID. I think it will still be possible to tell the difference using the menu ID and the item class.
    – Luis Sanz
    Commented Jul 19, 2016 at 9:42
  • Luis Sanz, what i need is to be able to catch the current-menu-item-id that i clicked on (from that specific menu), in all pages Commented Jul 19, 2016 at 9:50
  • Do you mean that when you navigate to a new page you need to know its referrer?
    – Luis Sanz
    Commented Jul 19, 2016 at 11:38

2 Answers 2


This was the solution to my problem:

Then what I would do is to store the clicked anchor in a cookie using javascript and read it while loading the new page. $_SERVER['HTTP_REFERER'] won't be reliable here. – Luis Sanz Jul 19 at 12:33


You could pass second argument to your function, which store menu name. Changes that you have to do:

add_filter( 'wp_nav_menu_objects', 'wpse16243_wp_nav_menu_objects', 10, 2 );  
function wpse16243_wp_nav_menu_objects( $sorted_menu_items, $args )

In function you will get menu name by $args['menu'] so

if ( $menu_item->current ) { 

should be changed to

if ( $menu_item->current && $args['menu'] == 'name_of_menu_you_want_to_search' ) { 

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.