Patients with advanced idiopathic pulmonary fibrosis (IPF), a complex and incurable lung disease with an elusive pathology, are nearly exclusive candidates for lung transplantation. Improved identification of patient subtypes can enhance early diagnosis and intervention, ultimately leading to better prognostic outcomes for patients. The goal of this study is to identify new immune patterns and biomarkers in patients. Immune subtypes in IPF patients were identified using single-sample gene set enrichment analysis, and immune subtype-related genes were explored using the weighted correlation network analysis algorithm. A machine learning integration framework was used to establish the optimal prognostic model, known as the immune-related risk score (IRS). Single-cell sequencing was conducted to investigate the major role of macrophage-derived PLA2G7 in the immune microenvironment. We assessed the stability of celecoxib in targeting PLA2G7 through molecular docking and surface plasmon resonance. IPF patients present two distinct immune subtypes, one characterized by immune activation and inflammation, and the other by immune suppression. IRS can predict the immune status and prognosis of IPF patients. Furthermore, multi-cohort analysis and single-cell sequencing analysis demonstrated the diagnostic and prognostic value of PLA2G7 derived from macrophages and its role in shaping the inflammatory immune microenvironment in IPF patients. Celecoxib could effectively and stably bind with PLA2G7. PLA2G7, as identified through IRS, demonstrates marked stability in diagnosing and predicting the prognosis of IPF patients as well as predicting their immune status. It can serve as a novel biomarker for IPF patients.