Had exactly the same problem with my 9 year old twin boys. Both toilet trained before nursery with the occasional night time and day time mishaps that, let's be honest, all kids have but they and us don't admit to.
Reception class had an absolute toilet training zealot for a teacher. Worked on a system of heavy punishment for poo and wee accidents. Any accident resulted in the child being left in the toilet alone for an hour, despite having clearly displayed their bowels or bladder were empty! Kids became terrified of having an accident where the poo pants accident at the park, or wet pants accident due to be being lazy or distracted was something we previously all laughed about as part of childhood life, but had become a major drama and crisis for them. They both started holding onto their poo, got the desentised rectum from the constant pressure, started bypassing incontinence, getting through up to ten pairs of underpants a day with constantly dribbling liquid poo and wee and smelling awful.
We tried everything, bowel and bladder nurses, the family doctor, paediatricians, Gi specialists, play therapists, a psychologist. We had regular clear outs with laxatives, x-rays, ultrasounds, scopes. To no avail.
To cut to the chase we've gotten through this by taking a step backwards. The crucial thing is to keep the poo moving, and focus on that. Regular bulking laxatives and occasionally stimulant laxatives are essential. Don't make the mistake of stopping them like we did. The urge to pass stool comes much less frequently, and must never be wasted. We had to advise both boys that if they got an urge to poo, they go to the toilet and poop straight away. If there isn't a toilet, then they're encouraged to deliberately poop in their pants (somewhere discrete if possible) which sounds gross, but it's far better than what went before. Same applies to pee. They are never ever chastised for doing this. By stopping the holding and stretching of their bowels, they are slowly getting their sensations back. We reward their behaviour by saying things like " oh you did a big poo, you must be getting better, I'm so happy for you and proud." We don't mention the dirty pants, and instead of being terrified of pooping himself, the child knows it's okay to do what's necessary for the bowel to heal, and pooping pants is not something to fear because of chastisement, anger, violence, exclusion etc.
Both kids are well on the way to recovery. 9 months on twin number 1 poops himself 2 or 3 times a week, the other 2 or 3 times a month. It was 5-10 x a day. They do proper poops now and know when they need to poop. It's back in their consciousness. They are putting on weight, look healthier, look so much happier. It's unorthodox as an approach but it's working. They day we went to the park, found the toilets were vandalised and my husband told them to just poop their pants and keep playing was a life-changing day. After months of misery, they came home in poopy pants but were happy and excited and looked like a lifetime of worry had been lifted off their little shoulders, and dare I say it, looked like kids again? They know now we won't be angry if they poop themselves 10x a day (they don't). Remember that underpants are expendable (I get mine from the supermarkets, in the sales etc and never seem to spend more than 30p a pair) but their large bowel has to last a lifetime. So the big message from me is to focus on the poop keeping moving and forget about the pants and make sure your son knows that too