A migrant from Honduras passes a child to her father after he jumped the border fence to get into the U.S. side in San Diego, from Tijuana, Mexico, on Jan. 3. (Daniel Ochoa De Olza/AP/file)

In a legal blow to the Trump administration, a federal judge ruled Friday that all migrant families separated during the government’s border crackdown should be included in a class-action lawsuit. But he stopped short of immediately ordering the Justice Department to track them all down.

U.S. District Judge Dana Sabraw in California said the universe of separated families should extend beyond the 2,700-plus children taken from their parents last spring, and include families forced apart as early as July 1, 2017, and the months afterward, when the Trump administration was denying that it had a policy of separating families.

Sabraw said a government watchdog report in January that potentially thousands more families were separated than the Trump administration had admitted publicly compelled the court to look into the matter.

“The hallmark of a civilized society is measured by how it treats its people and those within its borders,” he wrote in a 14-page ruling. “That Defendants may have to change course and undertake additional effort to address these issues does not render modification of the class definition unfair; it only serves to underscore the unquestionable importance of the effort and why it is necessary (and worthwhile).”

The ruling dramatically expands the scope of the class-action lawsuit that compelled the Trump administration to reunite the separated families and prolongs a political tangle for the president that had been nearing its end.

Sabraw said he would hear arguments later this month to decide whether to require the Trump administration to locate the rest of the families. But he said “the Court has an obligation to resolve that question,” especially for parents who may have been deported without their children, and although it “may be burdensome, it clearly can be done.”

Otherwise, he wrote, parents may have been deported without their children, creating the “very real possibility of a permanently orphaned child” because of the “questionable” actions of government officials.

The Justice Department did not immediately respond to the ruling.

The American Civil Liberties Union, which represents the parents in the lawsuit, cheered the decision and said it would ask Sabraw to order the Trump administration to provide the names and contact information of all the separated families, as it did for the ones split up last spring.

“The court powerfully stated that there’s a lot at stake here and that it was not prepared to let the government escape responsibility simply because it might be burdensome to track the families down,” said Lee Gelernt, lead attorney in the family separation lawsuit and deputy director of the ACLU’s Immigrants’ Rights Project.

More than 2,700 migrant children were separated from their parents from May 5 to June 20 in a controversial attempt to deter rising numbers of illegal border crossings, particularly of ­families, most of whom were released in the United States because of what the Trump administration called legal “loopholes.”

Homeland Security Secretary Kirstjen Nielsen and then-Attorney General Jeff Sessions opted to criminally prosecute all adults who crossed the border illegally, even if they were with children. Adults were detained, and their children were shuttled to shelters across the country.

Trump rescinded the crackdown on June 20 amid widespread protests, including from Republicans, that the separations were harming children.

Days later, Sabraw ordered the administration to put the families back together, which required combing through handwritten and electronic files to find children as young as babies and nearly 500 parents who had been deported without them.

Almost all the families had been reunited when the inspector general for the Department of Health and Human Services issued the report in January that surprised Sabraw and the ACLU. The report estimated that “thousands” of other children may have been taken from their parents, including from July 1, 2017, to November 2017, during a trial run of the crackdown in west Texas and New Mexico. Until then, the ACLU had heard of only a few such cases.

At a hearing last month, Justice Department lawyers said most of those children were probably with a parent or a close relative who had been vetted by the government, and expressed concern that contacting the families could traumatize the children. They suggested having parents file individual lawsuits to get their children back.

Sabraw, a Republican appointee in the Southern District of California, said in his ruling that such a requirement would make it “virtually impossible” for deported parents to get their children back.

The ACLU has noted that many parents hail from remote villages in Guatemala and other Central American nations and would face severe difficulties in finding their children.

In his ruling, Sabraw noted that lawmakers and government inspectors have been looking into the family separations, but he said that did not diminish the court’s obligations in the case.

“Clearly,” the judge wrote, “the matter of determining the ‘number and status of separated children’ who have entered [government] care at any point in time because of the Administration’s family separation policy is not going away.”

The lawsuit is Ms. L v. U.S. Immigration and Customs Enforcement in U.S. District Court for the Southern District of California.