The lawyer taking on US president Donald Trump over his alleged affair with an adult film actress has secured the release of a nine-year-old boy from Guatemala at an immigration court.

A judge had initially denied Michael Avenatti’s request to dismiss the removal case against the boy, whose mother was deported in June.

Mr Avenatti and another lawyer had offered to take the boy from court to the airport and fly him to Guatemala.

Instead, the judge agreed to let the boy voluntarily depart the US in the next 60 days, as part of the ordinary process when an immigrant child who entered the US without legal permission wishes to return.

Mr Avenatti said later on Twitter that following negotiations, authorities had allowed him to take the boy home to his mother in Guatemala.

Images posted by Mr Avenatti later showed the boy, named only as Antony, being reunited with his mother.

Standing outside court after the hearing earlier, Mr Avenatti denied that he was representing the boy as a publicity stunt.

He said: “I’ve been representing dozens of mothers and children for weeks now, travelling around the country. My record speaks for itself.”

‘Families belong together’ protests over Trump immigration policy Show all 8 1 /8 ‘Families belong together’ protests over Trump immigration policy ‘Families belong together’ protests over Trump immigration policy Mishawaka, Indiana AP ‘Families belong together’ protests over Trump immigration policy Washington DC AFP/Getty Images ‘Families belong together’ protests over Trump immigration policy New York Mayor Bill De Blasio speaks in Tornillo, Texas alongside the many other US mayors who have called for detained immigrant children to be reunited with their families AP ‘Families belong together’ protests over Trump immigration policy Albuquerque Mayor Tim Kelle leaves a teddy bear as a gift for detained immigrant children in Tornillo, Texas AP ‘Families belong together’ protests over Trump immigration policy Seattle Mayor Jenny Durkan and Los Angeles Mayor Eric Garcetti leave sandals as gifts for detained immigrant children in Tornillo, Texas AP ‘Families belong together’ protests over Trump immigration policy San Diego, California EPA ‘Families belong together’ protests over Trump immigration policy US embassy, Mexico City AFP/Getty Images ‘Families belong together’ protests over Trump immigration policy San Diego, California EPA

In addition to his legal fights on behalf of adult film actress Stormy Daniels, Mr Avenatti has taken up the cases of immigrant families separated under the Trump administration’s zero-tolerance policy on border crossings.

He has also started to draw some attention as a potential challenger to Mr Trump in the next presidential election.

Aside from Mr Avenatti’s notoriety, the Guatemalan boy’s case falls into the same legal situation as hundreds of children who were taken from their parents, only to have their parents removed without them.

The US government said last week that it counted 365 children in that category, out of 2,500 children who were separated before the Trump administration officially stopped the policy.

Four government departments submitted a plan last week on how it would reunify the families.

Government officials are supposed to track down parents in their countries of origin to confirm they want their children sent back and that there is no reason to stop reunification, then coordinate with those countries to return the children.

However, immigrant children without a parent in the US have their own legal rights, said Jennifer Podkul, director of policy at the legal group Kids in Need of Defence.

They can file claims to try to stay in the US even if their parents are no longer here, she said.

Ms Podkul added: “If a child wants to stay and make a claim for protection, we want to make sure ... that the child’s voice is always heard.”