'Adding dynamic submenus with wp_nav_menu_objects hook in Wordpress

I am having trouble adding dynamic submenu items in Wordpress. The first hook adds the items in the correct place, but the second hook adds the items to the main menu.

I am using the hook twice. The first time to add parent categories to the 'Products' menu. The second time to add child categories to the previously created sub-menu.

Whether I create two loops in one hook, or use two separate hooks the result is the same: the items added in the second loop show up at the end of the main menu, not in the sub-menu.

When I add to items[] within the same loop, the items do appear in the sub-menu, but not in the sub-sub-menu.

Any help would be appreciated.

add_filter( 'wp_nav_menu_objects', 'arb_wp_nav_menu_items', 10, 2 );
function arb_wp_nav_menu_items($items, $args) {
    $menu_count = count( $items ) + 1;
    $parent_id = 0;
    $cat_args = array(
        'taxonomy'  => 'product_cat',
        'orderby'    => 'name',
        'order'      => 'ASC',
        'hide_empty' => true,
    );
    $categories = get_terms($cat_args);
    foreach ( $items as $item ) {
        if ( $item->title === 'Products') {
            $parent_id = $item->ID;
            
            foreach($categories as $category) {
                $inc++;
                $children = get_term_children($category->term_id, 'product_cat');
                if (!empty( $children ) && !is_wp_error( $children ) ) {
                    $name = $category->name;
                    $items[] = (object) array(
                        'ID'                => $menu_count + 10000,
                        'title'             => $name,
                        'url'               => '#',
                        'menu_item_parent'  => $parent_id,
                        'menu_order'        => $menu_count,
                        'type'              => '',
                        'object'            => '',
                        'object_id'         => '',
                        'db_id'             => '',
                        'classes'           => '',
                        'post_title'        => '',
                    );
                    
                }
            }
            break;
        }
    }
    return $items;
}

add_filter( 'wp_nav_menu_objects', 'arb_wp_nav_submenu_items', 10, 2 );
function arb_wp_nav_submenu_items($items, $args) {
    $menu_count = count( $items ) + 1;
    $new_id = 0;
    $cat_args = array(
        'taxonomy'  => 'product_cat',
        'orderby'    => 'name',
        'order'      => 'ASC',
        'hide_empty' => true,
    );
    $categories = get_terms($cat_args);
    foreach($items as $child_item) {
        foreach ($categories as $child_category) {
            if ($child_item->title === $child_category->name) {
                $new_id = $child_item->ID;
                $term_children = get_term_children($child_category->term_id, 'product_cat');
                foreach ($term_children as $term_child) {
                    $term = get_term_by( 'id', $term_child, 'product_cat' );
                    
                    $items[] = (object) array(
                        'ID'                => $menu_count + 20000,
                        'title'             => $term->name,
                        'url'               => '#',
                        'menu_item_parent'  => $new_id,
                        'menu_order'        => $menu_count,
                        'type'              => '',
                        'object'            => '',
                        'object_id'         => '',
                        'db_id'             => '',
                        'classes'           => '',
                        'post_title'        => '',
                    );
                }
            }
        }
    }
    return $items;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source