Recent studies have shown a correlation between piglet diarrhea and the gut microbiota. However, the precise mechanism by which intestinal microorganisms and their metabolites influence diarrhea in weaned piglets remains unclear. This study explored differences in the gut microbiota and associated metabolites between healthy and diarrheic-weaned piglets using macrogenomic and metabolomic analyses. The histomorphological results showed that diarrheic piglets had shorter jejunal and ileal villi, some of which were shed, compared to healthy piglets. Substantial differences in gut microbial diversity and metabolites were also observed, with Bacteroidaceae bacterium and Caudoviricetes being the main differential organisms that were strongly correlated with host status. Microbial functions, mainly the metabolism of carbohydrates, glycans, lipids, and amino acids, as well as related enzyme activities, were substantially different. The major differential metabolites were carnosine, pantothenic acid (vitamin B5), pyridoxal, methylimidazoleacetic acid, indole-3-acetaldehyde, and 5-hydroxyindoleacetic acid. These metabolites were enriched in beta-alanine, histidine, tryptophan, and vitamin B6 metabolism, and in the pantothenate and CoA biosynthesis pathways. Combined macrogenomic and metabolomic analyses revealed that carnosine, vitamin B5, and pyridoxal were negatively correlated with Caudoviricetes; methylimidazoleacetic acid, indole-3-acetaldehyde, and 5-hydroxyindoleacetic acid were positively correlated with Caudoviricetes. Whereas carnosine and vitamin B5 were positively correlated with Bacteroidaceae bacterium, 5-hydroxyindoleacetic acid was negatively correlated. The decreased abundance of Bacteroidaceae bacterium and the increased abundance of Caudoviricetes and related metabolites likely contribute to post-weaning diarrhea in piglets. Therefore, the abundance of Bacteroidaceae bacterium and Caudoviricetes can likely serve as potential markers for identifying and preventing diarrhea in post-weaning piglets.