A US judge has blocked President Donald Trump's executive order that sought to withhold federal funds from so-called sanctuary cities, dealing another legal blow to the administration's efforts to toughen immigration enforcement.

US District Judge William Orrick in San Francisco issued the preliminary injunction in two lawsuits on Tuesday - one brought by the city of San Francisco, the other by Santa Clara County - saying the president has no authority to attach new conditions to federal spending.

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

White House chief of staff Reince Priebus said the ruling was the 9th Circuit "going bananas", referring to the judicial district where the ruling was issued.

The 9th Circuit Court of Appeals previously ruled against President Donald Trump's ban on travellers from several Muslim countries.

"We're taking action to appeal this," Priebus told reporters of the sanctuary city decision.

Judge Orrick said Trump cannot set new conditions for the federal grants at stake. And even if he could, the conditions would have to be clearly related to the funds at issue and not coercive, the judge said.

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

READ MORE: Can New York remain a sanctuary city?

Trump's January executive order on immigration ordered the Department of Homeland Security and justice department to block cities who do not cooperate with federal immigration enforcement from receiving federal funds.

Hundreds of cities, counties and states have been categorised as sanctuary jurisdictions in unofficial tallies, including the entire states of California, Connecticut, New Mexico and Colorado.

Supporters of the sanctuary policy argue that enlisting police cooperation in rounding up immigrants for removal undermines communities' trust in local police, particularly among Latinos.

The Trump administration contends that local authorities endanger public safety when they decline to hand over for deportation undocumented immigrants arrested for crimes.

San Francisco City Attorney Dennis Herrera praised the ruling on 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".

Santa Clara County sued in February, saying Trump's order was unconstitutional. San Francisco filed a similar lawsuit.

READ MORE: What happens during a deportation raid in the US?

The US Department of Justice threatened last week to cut some funding to California, as well as eight cities and counties across the United States.

The department singled out Chicago and New York as two cities "crumbling under the weight of illegal immigration and violent crime," even though New York City is experiencing its lowest crime levels in decades and experts say Chicago's recent spike in violent crime has little to do with illegal immigration.

Santa Clara County receives about $1.7bn in federal and federally dependent funds annually, about 35 percent of its total revenues.

The county argued it was owed millions of dollars in federal funding every day and that its budgetary planning process had been thrown into disarray by the order.

The justice department said the counties had taken an overly broad interpretation of the president's order, which it said would affect only justice department and homeland security funds, a fraction of the grant money received by the counties.

In his ruling, Orrick said the language of the order made it clear it sought to withhold funds beyond law enforcement.

"And if there was doubt about the scope of the order, the president and attorney general have erased it with their public comments," Orrick wrote.

The judge cited comments from Trump calling the order "a weapon" to use against jurisdictions that disagree with his immigration policies.

"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," Orrick wrote.

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