Less than a month ago, conservative Judge Brian Hagedorn was a dead man walking.

As he raced against liberal Judge Lisa Neubauer for a seat on the Wisconsin Supreme Court, the local press portrayed him as an anti-gay extremist and bigot. They called out the Christian school he founded for its code of conduct stipulating “no sex outside of marriage between a man and a woman.”

Never mind that this code of conduct is common practice at private religious schools across the country.

The Democrats seized on the moment. Outside money poured in from groups like Planned Parenthood and former Attorney General Eric Holder’s National Democratic Redistricting Committee, funding ads that portrayed Hagedorn as a radical. Meanwhile, Hagedorn’s biggest donor, the Wisconsin Realtors Association, demanded he return their contribution because his beliefs conflicted with the values of its members. Other traditional GOP corporate donors like the Chamber of Commerce ghosted him.

That’s when things got interesting.

Conservative activist Mary Magdalen Moser, a poll worker for the Republicans, sensed a turn in the tide. She was infuriated by the media’s treatment of Hagedorn — and she knew the populist coalition that put Trump in office would be electrified.

“Wisconsinites have always been a very open bunch,” she told me. “As long as you stay out of our way, we let you do your own thing. People were incensed that Neubauer went after Hagedorn’s faith.”

Despite the bad press, Hagedorn waged a tireless grass-roots effort to personally tell his story every day across the state.

Finally, in the last week before the election, the Republican State Leadership Committee delivered a cash Hail Mary for Hagedorn. Their million-dollar ads prominently showcased President Trump as a booster of the conservative Supreme Court, saying he is “defending our rights, all thanks to your vote.” The ads also noted that extreme out-of-state special-interest groups were spending millions to back liberal Neubauer and “spreading false attacks against conservative, rule-of-law Judge Brian Hagedorn, just like they did against Justice [Brett] Kavanaugh.”

This reminder of Trump’s record on conservative justices, twinned with voters sympathetic to the issue of religious liberty, delivered a one-two punch.

What do you do when you have all the money in the world, the media and culture on your side and you still struggle to win over voters?

Against all odds, Hagedorn won the state, including two Democratic-leaning counties, Racine and Kenosha — just like Trump did in 2016.

“Trump and Hagedorn won because they didn’t back down,” said Moser, a 56-year-old who lives in Kenosha. Though she grew up in a Democratic home and has never registered with either party, she calls the treatment of Hagedorn a tipping point.

“Freedom of religion cannot be perverted into freedom from religion,” Moser said. “Tolerance must remain a two-way street, especially in our judicial branch.”

As the Democratic Party shifts further left, they need to face a question: What do you do when you have all the money in the world, the media and culture on your side and you still struggle to win over voters?

But the Republicans, too, must face a question: In the age of the online bully, how do you woo business donors scared off from supporting socially conservative candidates?

Still, one thing is entirely clear: If you run on the platform of defending religious freedom, it can be a winner — even among voters who aren’t traditional conservatives.

And, for many, the issue of conservative justices is also a slam dunk.

“Trump’s ability to deliver conservative judges is a major factor in galvanizing Trump’s coalition,” said retired Wisconsin state Supreme Court Justice Michael Gableman, who Hagedorn once clerked for. “Anyone who doesn’t think that’s a winning message for him in 2020 still isn’t paying attention to a big part of this country.”

The lessons of this statewide election will likely resonate with Americans well beyond its (once true-blue) borders. Wisconsin is now officially a swing state, and what happens here will likely determine who occupies the White House come 2021.

Salena Zito is the co-author of “The Great Revolt: Inside the Populist Coalition Reshaping American Politics.”