Swine wastewater pretreated by solid-liquid separation was optimized for biogas production and water purification. Dynamic diversity of the bacterial community in the anaerobic plug flow reactor was investigated under various temperatures and hydraulic retention times (HRT). Results of batch experiments indicated that under optimal operating conditions, biogas production, methane production rate, and chemical oxygen demand (COD) removal efficiencies increased up to 26%, 52%, and 44%, respectively. In the continuous experiments, optimal operating conditions for the plug flow operator were found to be HRT of 8 d and temperature of 35°C, under which relatively high CH4 concentration (67%), CH4 production rate (4.6 l/d), and total COD removal (79%) were achieved. A minimum of 12 bands was observed in the denaturing gradient gel electrophoresis profile. Phylogenetic analysis revealed the predominance of phyla Firmicutes, Proteobacteria, and Euryarchaeota in the anaerobic plug-flow reactor. However, the occurrence and prevalence of specific bacterial species varied with HRT and temperature. This study establishes the optimal operating parameters for the anaerobic plug-flow digestion of swine wastewater, which can be employed in a full-scale reactor to maximize biogas production and to purify water quality.