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 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 said.

Get Breaking News Delivered to Your Inbox

“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.

In a statement provided to CBS News, the Justice Department points to the fact that it can still withhold federal grants from “sanctuary cities”:

The Department of Justice previously stated to the Court, and reiterates now, that it will follow the law with respect to regulation of sanctuary jurisdictions. Accordingly, the Department will continue to enforce existing grant conditions and will continue to enforce [the executive order]. Further, the order does not purport to enjoin the Department’s independent legal authority to enforce the requirements of federal law applicable to communities that violate federal immigration law or federal grant conditions.

It was the third major setback for the administration on immigration policy.

White House Chief of Staff Reince Priebus says the ruling is another case of “the 9th Circuit going bananas” in response to the 9th Circuit Court based in San Francisco, where judges have also ruled against Trump’s travel bans.

The administration has often criticized the 9th U.S. Circuit Court of Appeals. Orrick does not sit on that court but his district is in the territory of the appeals court, which has ruled against one version of Trump’s travel ban.

“The idea that an agency can’t put in some reasonable restriction on how some of these moneys are spent is something that will be overturned eventually, and we will win at the Supreme Court level at some point,” Priebus said.

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.

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, 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.”

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 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 Mr. Trump 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 also has 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 suits were the first to get a hearing before a judge.

Moina Shaiq holds a sign at a rally outside of City Hall in San Francisco, Wed., Jan. 25, 2017. President Trump moved aggressively to tighten the nation’s immigration controls Wednesday, signing executive actions to jumpstart construction of his promised U.S.-Mexico border wall and cut federal grants for immigrant-protecting “sanctuary cities.” AP

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 Mr. 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.

Sanctuary cities offer safe harbor for undocumented immigrants who might otherwise be deported by federal immigration law enforcement officials. There are over 140 sanctuary jurisdictions -- cities and counties -- across the U.S., including at least 37 cities -- San Francisco, New York, Chicago, Seattle, Miami and Los Angeles, among others.

In 2016, a Justice Department inspector general’s report investigated how much in Justice Department federal grants some sanctuary jurisdictions receive (as of Mar. 2016). Over 60 percent of the funding goes to 10 jurisdictions identified by the report: