Last week a federal judge in California rejected the lawsuits filed by the cities of Oakland and San Francisco against the world’s largest fossil-fuel companies for allegedly causing climate change. The ruling deals a stunning blow to the mayors — like Mayor Bill de Blasio — and private law firms who have waged a coordinated attack on the big oil companies in courtrooms around the country.

But will those profit-seeking lawyers and the mayors they recruit learn their lesson? Let’s hope so.

If they prevail, the outcome will enrich lawyers to the detriment of energy consumers everywhere.

Contingency-fee lawyers in search of the next big litigation jackpot are attempting to politicize the civil justice system. They’ve set their sights on deep pockets and an important component of the global economy — oil. But, as with all political debates, climate policy should be decided in the statehouse, not the courthouse.

And this is a reminder that the process by which cities hire private lawyers should be more transparent. A bidding process like all other government contracts should apply to hiring outside counsel. Taxpayers are right to be concerned whether their best interests are represented. For the suit filed in New York, de Blasio’s office has yet to release information about how much the lawyers stand to profit should the case ever settle.

The good news: The opinion issued last Monday by California District Judge William Alsup, a Clinton appointee, thoroughly refutes all of the far-reaching legal arguments made by lawyers from powerhouse plaintiffs law firm Hagens Berman Sobol Shapiro, which represented Oakland and San Francisco. This decision provides a roadmap to dismissal for federal judges considering similar cases in other jurisdictions.

Cookie-cutter lawsuits targeting BP, Chevron, ConocoPhillips, ExxonMobil and Royal Dutch Shell involving the same lawyers were filed in New York and Seattle. They claim these five corporations created a public nuisance by supplying the greenhouse gas-emitting products that fuel our vehicles and keep us warm in the winter.

At a June 13 hearing on whether to dismiss the de Blasio administration’s claim against the oil companies, Southern District of New York Judge John Keenan questioned the claims — and pointed out some glaring liberal hypocrisy from Team Blas.

If these energy producers are truly ruining the planet, “does the city have clean hands?” the judge asked. “The firehouses all have trucks. The Sanitation Department has trucks. If you open the door and go out to Foley Square, you’re going to see five police cars.”

The suits allege that a select few oil and gas firms are fully responsible for causing global climate change and should pay out billions of dollars for any current and future climate-related damages.

But how could a court ever quantify how a single energy producer impacted or will impact the environment?

In Alsup’s decision to dismiss the California suits, he notes that the plaintiffs’ theory falls apart on recognition that emissions come from the use of fossil fuels, not the sale or production of these products. Does this mean the real culprit for alleged climate-wrecking fuel consumption is the municipalities and their citizens themselves?

And Alsup makes a point of highlighting the undeniable benefits fossil fuels have brought to the world. He accepts that climate change is a real problem, but “the problem deserves a solution on a more vast scale than can be supplied by a district judge or jury in a public-nuisance case.”

Indeed, regulation by litigation is the wrong way to fix anything. Real policy decisions should be thought through carefully by government officials who are accountable to the people, not by unelected lawyers and mayors hungry for their moment in the national spotlight.

Luckily judges on both sides of the country have begun to see these lawsuits as part of the meritless p.r. campaign they truly are. Hopefully, scrutiny from the courts will lead lawyers to think twice before they attempt to stretch another novel legal theory to capitalize on controversy and exploit cities as a sympathetic plaintiff.

It’s incumbent on courts around the country to follow Alsup’s example and consistently reject this attempt by unelected, private lawyers to usurp the power of legislative bodies and cash in on a matter as crucial and complex as climate change.

And de Blasio, who benefits tremendously from the use of fossil fuels while cynically demonizing them to advance his career, should take a hint and let this one go.

Adam Morey is public affairs manager at the Lawsuit Reform Alliance of New York.