I got the answer, here it is: I want to add submenus ffrom database to the menu named ‘Products’
Create a custom plugin and install it through Admin panel. Inside the functions.php write this code. This is upgrade safe way and will not brake if the theme is updated.
add_filter( 'wp_nav_menu_objects', 'ravs_add_menu_parent_class' );
function ravs_add_menu_parent_class( $items ) {
$results="SOME SQL QUERY";
foreach ( $items as $item ) {
$title = $item->title;
$ID = $item->ID;
if($title=='Products')
$parentId = $ID;
}
foreach ( $results as $result ) {
$name = $result->name;
$id = $result->id;
$link = array (
'title' => $name,
'menu_item_parent' => $parentId,
'ID' => $id,
'db_id' => $id,
'url' => 'URL'
);
$items[] = (object) $link;
}
return $items;
}