You can use the second input argument $args
to target the specific menu.
Here’s an example with some checks that you can adjust to your needs:
add_filter( 'wp_nav_menu_objects', function( $sorted_menu_items, $args )
{
if(
'primary' === $args->theme_location // Alternative
&& 'some_menu_slug' === $args->menu->slug // Alternative
) {
// Your stuff here ...
}
return $sorted_menu_items;
}, 10, 2 );
When we add a menu item, a new row is created in the wp_posts
table. This is the ID you’re referring to. When we delete it and add it again, then we will get a new ID. So it might be better to target the menu theme location or the menu slug instead.
When you append the custom menu items in your example and the menu_item_parent
doesn’t exists, in the current menu, then it should be added to the menu as an item with menu_item_parent = 0
.