SAN FRANCISCO (CBSNewYork/AP) — A federal judge on Tuesday blocked President Donald Trump’s attempt to withhold funding from “sanctuary cities” that do not cooperate with U.S. immigration officials, saying the president has no authority to attach new conditions to federal spending.

U.S. District Judge William Orrick issued the preliminary injunction in two lawsuits — one brought by the city of San Francisco, the other by Santa Clara County — against an executive order targeting communities that protect immigrants from deportation.

The injunction will stay in place while the lawsuits work their way through court.

The judge rejected the administration’s argument that the executive order applies only to a relatively small pot of money and said Trump cannot set new conditions on spending approved by Congress.

“Federal funding that bears no meaningful relationship to immigration enforcement cannot be threatened merely because a jurisdiction chooses an immigration enforcement strategy of which the president disapproves,” the judge said.

It was the third major setback for the administration on immigration policy. The Justice Department had no immediate comment.

San Francisco City Attorney Dennis Herrera praised the ruling Tuesday and said the president was “forced to back down.”

“This is why we have courts — to halt the overreach of a president and an attorney general who either don’t understand the Constitution or chose to ignore it,” Herrera said in a statement.

Santa Clara County Counsel James Williams said the ruling will allow cities and counties across the country to prepare budgets without the “unconstitutional threat of federal defunding hanging over our heads.”

A Justice Department attorney, Chad Readler, previously defended the president’s executive order as an attempt to use his “bully pulpit” to “encourage communities and states to comply with the law.”

Readler also said the order applied to only three Justice Department and Homeland Security grants that would affect less than $1 million for Santa Clara County and possibly no money for San Francisco.

But the judge said executive order was written broadly to reach all federal grants and potentially jeopardized hundreds of millions of dollars in funding to San Francisco and Santa Clara.

He cited comments by the president and Attorney General Jeff Sessions as evidence of the order’s scope and said the president himself had called it a “weapon” to use against recalcitrant cities.

Last week, the Trump administration announced its plans to crack down on the so-called sanctuary cities – of which New York is one.

The Justice Department announced it would force the nine communities to prove they are complying with an immigration law by June 30 to continue receiving coveted law enforcement grant money.

It is an extension of Attorney General Jeff Sessions’ repeated threats to crack down on sanctuary communities by denying or stripping them of grant money.

The DOJ ordered officials in the nine cities to provide proof from an attorney that they are following the law.

In a news release last week, the Department of Justice said many of the jurisdictions are “crumbling under the weight of illegal immigration and violent crime.”

“New York City continues to see gang murder after gang murder, the predictable consequence of the city’s ‘soft on crime’ stance,” the release said.

Mayor Bill de Blasio’s Press Secretary Eric Phillips tweeted in the wake of the order, “Yeah, in fact, the NYPD is so soft on crime it has figured out how to prevent it better than anyone else in the nation.”

Yeah, in fact, the NYPD is so soft on crime it has figured out how to prevent it better than anyone else in the nation. https://t.co/SJ6bRuEAIe — Eric Phillips (@EricFPhillips) April 21, 2017

De Blasio and police Commissioner James O’Neill also both personally slammed the DOJ’s remarks.

The mayor released a statement following Tuesday’s ruling, commending the court for telling the Trump administration, as he put it, “No you can’t.”

“The president is going beyond his authority when he tries to cut vital funding to cities that don’t share his illogical and unconstitutional desire to scapegoat immigrants,” de Blasio said. “New York is the safest big city in America because we work with all our residents, not against some of them. We said from the beginning that a stroke of a pen in Washington would not change our values or how we protect our people.”

The order has also led to lawsuits by Seattle; two Massachusetts cities, Lawrence and Chelsea; and a third San Francisco Bay Area government, the city of Richmond. The San Francisco and Santa Clara County lawsuits were the first to get a hearing before a judge.

(© Copyright 2017 CBS Broadcasting Inc. All Rights Reserved. The Associated Press contributed to this report.)