Barriers to adequate food intake among hospitalised patients with nutrition-related disease conditions such as diabetes, cancer, kidney disease, and hypertension, are multifactorial and complex. Due to this, these disease conditions require multi-level interventions, including a change in the attitude and improved awareness towards food among healthcare staff and hospitalised patients. The priority interventions need to be feasible in practice, particularly in terms of the availability, affordability, accessibility, and time. This can be successful when specific food-based dietary guidelines for each disease condition are in place. Unfortunately, specific dietary guidelines for managing nutrition-related chronic disease conditions among the hospitalised patients are currently not well established in Tanzania. This significantly contributes to delayed recovery of patients and increased economic burden to families and communities due to increased medical costs resulting from prolonged length of hospital stays, readmission, and mortality. Our study aimed to develop context-specific food-based dietary guidelines which favour healthy eating in the light of gut microbiome to guide health care professionals and nutritionists to make rational decisions in planning diets for these patients. A cross-sectional study was conducted to collect data on dietary intake, dietary patterns, food price and availability to inform the formulation of specific food-based dietary guidelines for hospitalised patients in Tanzania. Moreover, a 7-day weighed food record (WFR) was used to assess dietary intake data among 400 hospitalised patients with nutrition-related chronic diseases. Likewise, data on prices of commonly consumed foods were obtained from hospitals’ requisition books, nearby markets and shops. Furthermore, a linear goal programming was used to optimize dietary intake patterns for hospitalised patients with nutrition-related disease conditions by developing food-based dietary guidelines based on the study’s context. The analysis showed that whole grains mainly dominated the observed hospital dietary patterns. However, diet optimization using linear programming (LP) provided adequate dietary patterns with nutrient-dense foods for each food group. The LP findings informed the process of formulating context-specific optimal dietary guidelines that use culturally acceptable food ingredients, and which favour healthy eating in the light of gut microbiota for managing nutrition-related chronic diseases for hospitalised patients.