I have a main menu which features items for each of my service cpts.
Each of those cpts has another menu associated with it, the id of which I grab using the ACF get_field function.
What I'm trying to do is insert the items from these service menus as sub-menus under the service's link in the main menu.
I'm using the wp_get_nav_items filter, but I can't seem to get the format right. I figured I could just append the items from the service menus to the array of main-menu items changing the menu_item_parent. However, this doesn't seem to work. It ends up simply appending all the items at the top level.
function custom_insert_service_menus($items, $menu, $args){
if($menu->slug != 'main-menu') return $items; // only for main-menu
$new_items = [];
foreach($items as $item){
if($item->object == 'service'){
$menu_id = get_field('menu', $item->object_id);
if(!empty($menu_id)){ // no menu so skip item
$service_menu_items = wp_get_nav_menu_items($menu_id);
foreach($service_menu_items as $service_menu_item){
if($service_menu_item->menu_item_parent == 0){
$service_menu_item->menu_item_parent = $item->ID;
}
$new_items[] = $service_menu_item;
}
}
}
}
$combined_items = array_merge($items, $new_items);
return $combined_items;
}
add_filter('wp_get_nav_menu_items', 'custom_insert_service_menus',null,3);