SAN FRANCISCO — A federal judge on Tuesday blocked any attempt by the Trump administration 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 order applies only to a relatively small pot of money and said President Donald Trump cannot set new conditions on spending approved by Congress.



Even if the president could do so, those conditions would have to be clearly related to the funds at issue and not coercive, as the executive order appears to be, Orrick aid.



"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 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," he said in a statement.



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



And he 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 disagreed, saying the 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 that the order was intended to target a wide array of federal funding. He said the president himself had called it a "weapon" to use against recalcitrant cities.



The government hasn't cut off any money yet or declared any communities to be sanctuary cities. But the Justice Department sent letters last week warning communities to prove they are in compliance. California was told it could lose $18.2 million.



"Sanctuary cities" is a loosely defined term for jurisdictions that don't comply with immigration authorities.



The Trump administration argued that the executive order applied narrowly to cities that forbid officials to report a person's immigration status to federal authorities. Orrick said it could also be construed to apply to cities that refuse to hold jail inmates for immigration authorities.



The Trump administration says that sanctuary cities allow dangerous criminals back on the street and that the order is needed to keep the country safe. San Francisco and other sanctuary cities say turning local police into immigration officers erodes the trust that is needed to get people to report crime.



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.



On Tuesday, mayors from several cities threatened with the loss of federal grants emerged from a meeting with Sessions saying they remain confused about how to prove their police are in compliance with immigration policies — a necessary step for them to receive grant money.



The sanctuary city order was among a flurry of immigration measures Trump has signed since taking office in January, including a ban on travelers from seven Muslim-majority countries and a directive calling for a wall on the Mexican border.



A federal appeals court blocked the travel ban. The administration then revised it, but the new version also is stalled in court.