@@ -113,6 +113,19 @@ function wp_api_v2_menus_dna_test( &$parents, $child ) {
113113 return false ;
114114}
115115
116+ /**
117+ * Search object in an array by ID
118+ */
119+ function find_object_by_id ( $ array , $ id ) {
120+ foreach ( $ array as $ element ) {
121+ if ( $ id == $ element ->ID ) {
122+ return $ element ;
123+ }
124+ }
125+
126+ return false ;
127+ }
128+
116129/**
117130 * Retrieve items for a specific menu
118131 *
@@ -122,6 +135,7 @@ function wp_api_v2_menus_dna_test( &$parents, $child ) {
122135 */
123136function wp_api_v2_menus_get_menu_items ( $ id ) {
124137 $ menu_items = wp_get_nav_menu_items ( $ id );
138+ $ all_menu_items = $ menu_items ;
125139
126140 // check if there is acf installed
127141 if ( class_exists ( 'acf ' ) ) {
@@ -173,7 +187,13 @@ function wp_api_v2_menus_get_menu_items( $id ) {
173187 // push child items into their parent item in the original object
174188 do {
175189 foreach ($ child_items as $ key => $ child_item ) {
176- if (wp_api_v2_menus_dna_test ($ menu_items , $ child_item )) {
190+ $ parent = find_object_by_id ( $ all_menu_items , $ child_item ->menu_item_parent );
191+
192+ if ( empty ( $ parent ) ) {
193+ unset($ child_items [$ key ]);
194+ }
195+
196+ else if (wp_api_v2_menus_dna_test ($ menu_items , $ child_item )) {
177197 unset($ child_items [$ key ]);
178198 }
179199 }
0 commit comments