Both of my girls did this before they were potty trained. I know it sounds mean, but what I finally did was when I saw that they had to poop, I'd put them on the potty and hold them there until they went. They would fight me, but I knew it would feel so much worse for them if they continued to be constipated. So I just ignored that they were unhappy about it and held them there until they went. (Neither one has had any kind of lasting "issues" with the potty since then, so overall I don't think this was all that traumatic for them.)
Of course it was painful for them at first because they were so "backed up" and constipated from having held it in so long. But after a few days of making them poop on the potty, they felt a lot better. Also during that time I kept giving them Miralax and/or mineral oil to make the poop softer and easier for them to pass. You may need to keep him on Miralax for 3-6 months or so, until he doesn't have any more issues.
One "trick" I used during potty training was to take my girls' diapers off and let them run around naked from the waist down. Then I watched them very carefully to see when they needed to go potty, and ran them to the toilet. After a couple of days of that, they understood what they needed to do.
Good luck!