  A search is performed for the rare decay W$^\pm\to\pi^\pm\gamma$ in
proton-proton collisions at $\sqrt{s} =$ 13 TeV. Data corresponding to an on W
integrated luminosity of 137 fb$^{-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$^\pm\to\pi^\pm\gamma$ 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$^\pm\to\pi^\pm\gamma$ 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$^\pm\to\pi^\pm\gamma$ decay is not observed. An upper exclusion limit is set
to this branching fraction, corresponding to 1.50 $\times$ 10$^{-5}$ at 95%
confidence level, whereas the expected upper limit exclusion limit is 0.85
$^{+0.52}_{-0.29}$ $\times$ 10$^{-5}$.
