Kids do this at times and it becomes a mental thing and also a mental game. My two nieces did this to their parents and it was the strangest thing. They potty trained and had no problems peeing in the toilet but they would hold back the poop and refuse to go at all. One of them did this so badly that it actually caused an obstruction and it landed her in the hospital from the impacted stool in the bowel. At that point, they ended up taking their child to a therapist to speak about this. It had something to do with issues of control and how she was trying to put all her efforts of controlling what she did and when into her bowel movements. To her, it was the one thing that she didn't have to do if she didn't want to. I know.....?????....strange, but this was the big epiphany that came out during the therapy. Of course, her younger sister saw all that was going on and all the attention that was focused on it and she started doing the same thing. Now they had two kids with this issue at the same time. It was unreal but you're not alone and I wanted to pass this on to you so you know it.
My youngest daughter used to hide in corners or go under a table when she had to poop and was also giving me issues about pooping in the potty. Granted, she was 22 months and potty training but when it came to poop, she gave me trouble. If she pooped her pants I would have her help me clean everything and also dump the poop that she made into the toilet no matter what. Then I would say, "next time, the poop should just go in the toilet so we don't have to clean." I saw her heading to the corner one day so I grabbed her, she screamed, I put her on the potty and kept her there until the poop fell into the bowl. She didn't like it but I figured if we could just get this done ONCE it might be over! It worked. After she got finished crying and acting up, I just lavished her with praise and made such a big deal over it that she started laughing and smiling through the tears. It never happened again. From that day on, she made it to the toilet and had no problems. I must have gotten her over the hump of her fears or whatever they were and I also broke the habit cycle of her hiding under the table or in a corner. If I saw her heading there, I'd redirect her right to the toilet. Sometimes a little tough love followed by support and praise is all that's needed. My daughter was not damaged by me keeping her on the toilet and insisting we get over this.....it actually worked!