To hear the common usage of this word (in the phrase 'shut your pie hole) you'd have to imagine it must have come to mean 'mouth' simply because that is the 'hole' (e.g. bodily orifice) which one would use to consume pie. But in actuality, the word 'pie-hole' derives from an Eighteenth Century Scottish term for the holes through which laces were drawn in boots -- which openings were initially simply called an eyelets or 'eye-holes,' owing to their similarity to the eye of a needle. And which phrase probably got changed over from eye-hole to pie-hole simply owing to a rhythmic similarity of sound. The 'p' it is noted, might have come from pig or pyg, which resembled the Danish word for a prick (meaning a small hole, bootlace holes being made by using a leather punch to 'prick' the boot). And so by the mid Nineteenth Century the word 'pie hole,' because it represented this little tiny hole for shoe-lacing, came to be used euphemistically among the Scottish to reference any really small hole, especially anything which would be a tight fit for a person to squeeze through for example (as in, the boy could barely squeeze through that pie-hole in the fence). Now, when and how it came to refer to the mouth (and especially to the mouth which somebody was commanding to be shut), well that remains a mystery. But you can bet it began with the Scottish.

One additional note on usage: it appears that when instructing somebody to shut their pie hole, it is most amusing to pronounce/spell the possessive pronoun as "yer" -- as in, "shut yer pie hole, ye scalliwag!!"

Reference:
An etymological dictionary of the Scottish language: illustrating the words in their different significations. Volume 2. By John Jamieson, Edinburgh, 1808.