© 2021 The Author(s)A search is performed for the rare decay W±→π±γ in proton-proton collisions at s=13TeV. Data corresponding to an integrated luminosity of 137fb−1 were collected during 2016 to 2018 with the CMS detector. This analysis exploits a novel search strategy based on W boson production in top quark pair events. An inclusive search for the W±→π±γ decay is not optimal at the LHC because of the high trigger thresholds. Instead, a trigger selection is exploited in which the W boson originating from one of the top quarks is used to tag the event in a leptonic decay. The W boson emerging from the other top quark is used to search for the W±→π±γ signature. Such decays are characterized by an isolated track pointing to a large energy deposit, and by an isolated photon of large transverse momentum. The presence of b quark jets reduces the background from the hadronization of light-flavor quarks and gluons. The W±→π±γ decay is not observed. An upper exclusion limit is set to this branching fraction, corresponding to 1.50×10−5 at 95% confidence level, whereas the expected upper exclusion limit is 0.85−0.29+0.52×10−5.