US border authorities have 30 days to reunite children separated from their parents at the border, a judge in California has ruled.
More than 2,000 children have been taken away from their parents at the US border with Mexico under the Trump administration's "zero tolerance" immigration policy.
US district judge Dana Sabraw, who was appointed by previous Republican president George W Bush, said that children under five must be reunited with their parents in under 14 days and the government should provide phone contact between all separated children and their parents within 10 days.
He also issued a nationwide injunction on future separations, unless the parent is unfit or does not want to be with the child.
Following an international outcry, Donald Trump last week issued an executive order to stop the separations, saying that children and their parents would be detained together instead.
But Judge Sabraw said the order's language was "not absolute", especially as it mentioned no plan for reuniting those families already affected.
He said: "Placing the burden on the parents to find and request reunification with their children under the circumstances presented here is backwards.
"When children are separated from their parents under these circumstances, the government has an affirmative obligation to track and promptly reunify these family members."
Many of the children affected were placed in government contracted shelters, some hundreds of miles away from where their parents were held after arriving in the US.
Judge Sabraw's ruling states that the government had not been prepared to accommodate the "mass influx" of separated children after the immigration policy was announced by Attorney General Jeff Sessions in May.
There was no reunification plan and some parents had been deported at different times and from different locations than their children, he added.
The case was brought by a woman identified only as Ms L, who fled the Democratic Republic of Congo seeking asylum.
She was separated from her six-year-old daughter, with the little girl being placed in a facility in Chicago hundreds of miles from her mother.
Responding to the judge's ruling, American Civil Liberties Union lawyer Lee Gelernt said: "Tears will be flowing in detention centres across the country when the families learn they will be reunited."
The justice and homeland security departments did not immediately comment.
More from Donald Trump
Health and human services secretary Alex Azar told Congress on Tuesday that the number of children in custody after being separated from their parents has gone down by only six in the past week.
Judge Sabraw's decision comes as 17 states sued the Trump administration to force it to reunite the families, arguing that they are being forced to shoulder increased child welfare, education and social services costs as a result of the policy.