The NYPD and MTA came up with the perfect way to catch a President Trump-loving graffiti artist: They built a wall.

Resourceful transit cops set up a plywood “dummy wall” to hide behind in a Brooklyn subway station and simply waited for their suspect to come to them, police said Tuesday.

Officers from Transit District 30 took the unusual lmeasure after being flooded with complaints that someone had been scrawling the phrase “#lovetrump” in Sharpie in a stairwell near the R-train platform at the Borough Hall/Court Street station. The vandal had allegedly hit twice since Feb. 9, according to police.

The wall worked like a charm, and cops snared Jamie Montemarano, 43, on vandalism charges.

“Sure enough, our man came back and put up again ‘#lovetrump,’” said NYPD Chief of Transit Edward Delatorre. “They caught him live, writing on the beam [in the stairwell].”

MTA workers physically built the wall, according to Delatorre.

“The officers who know the station well asked if they could put up a temporary wall, which would create an enclosure or room,” the chief said.

The plywood barrier — similar to the temporary walls erected around stations undergoing repairs, including a padlocked door for worker access — went up on March 12 at the bottom of the stairwell, according to Delatorre.

Cops in the enclosure hit pay dirt just three days later when they spied Montemarano marking up a metal beam along the stairwell with his pro-Trump tag, Delatorre said.

“The officers hid behind the wall and when the perp came back, they jumped out and grabbed him,” he said.

Montemarano, who has no prior arrests, was cut loose with a desk-appearance ticket for charges including graffiti and criminal mischief.

Montemarano, who was not charged in the first two incidents, said Tuesday he never saw the trap coming.

“I didn’t know it was a sting,” he said when reached by phone. “This was just a stupid thing to do.”

The latest scrawl could still be seen on the station beam Tuesday afternoon, but officials said it would be painted over “immediately.”

“We take graffiti very seriously and work closely with our law-enforcement partners to prevent it in the subway system,” an MTA spokesman said.

Additional reporting by Joe Marino and Danielle Furfaro