The judge ordered Rey to go to Larned State Hospital. He'll be there for 90 days to get treatment.
The 35-year-old is accused of dismembering his wife's body in front of their two children in a Kansas City hotel room. He said Jessica Rey died from childbirth.
The child endangerment charge came after police said they found Rey and his children living in a Lenexa U-Haul storage unit with his wife's remains in a cooler in October.
When he was arrested, police said Rey told them his wife delivered their baby in the bath tub of a hotel room near Interstate 70 and Stadium Drive, where they'd been staying since Sept. 25.
He told them he used a plastic fastener and a shoe string to secure the umbilical cord. He told police she died after childbirth and he placed her in the bed and then took pictures with his wife and kids together.
He said he spent two days with her body, Friday and Saturday, before dismembering her and then taking her body in the cooler to the storage facility in Lenexa.
Detectives said Rey admitted to cutting up his wife in the bathtub and then putting her body parts in the cooler. What didn't fit in the cooler, he said he flushed down the toilet or boiled in a pot on the stove.
Lenexa police were tipped off about Rey after the U-haul Storage Facility contacted them. They said he was suspiciously talking about his wife dying while giving birth to their newborn. When police found him at the storage facility, they said he told them his wife had died from suicide after giving birth.
"The suspect spontaneously informed the officers his wife was inside the cooler and looked to the direction of a red and white Igloo style cooler with wheels," the probable cause statement said.