From 1558 to 1829, Catholics in England were prohibited from practicing their faith at all. Possessing or professing anything Catholic could get you killed in any of a number of horrible ways. In response, the beleaguered believers wrote "The Twelve Days of Christmas" as a covert way to teach the catechism. The song's "true love" is God, "me" is the believer, and each gift has spiritual significance:
1 Partridge in pear tree = Jesus on the cross
2 Turtle doves = Old and New Testaments
3 French hens = Faith, hope, and charity
4 Calling birds = Four Gospels and/or four evangelists
5 Golden rings = The Pentateuch
6 Geese a-laying = Six days of creation
7 Swans a-swimming = Seven gifts of the Holy Spirit
8 Maids a-milking = Eight Beatitudes
9 Ladies dancing = Nine fruits of the Spirit
10 Lords a-leaping = Ten Commandments
11 Pipers piping = Eleven faithful apostles
12 Drummers drumming = Twelve points of doctrine in the Apostle's Creed
Like the candy cane story, this tale begins with a skewed version of English history. In 1558, England's Catholic queen, "Bloody Mary" Tudor, was succeeded by her Protestant half-sister, Elizabeth. Though Protestants were now in power, and open practice of Catholicism was forbidden by law (until the Emancipation Act of 1829), this was hardly a death sentence for all the country's Catholics. There were periods of intense persecution but also periods of relative leniency, including much of Elizabeth's reign, during which the Via Media ("Middle Way") between Catholic and Protestant practices held sway. But even when the situation for Catholics in England was truly bleak, they never needed to be secretive about the truths supposedly hidden in "The Twelve Days of Christmas," because all of those ideas are held by Protestants as well.
Other arguments also challenge the hidden catechism theory. One, merely remembering the number of gospels, Beatitudes, and commandments isn't enormously helpful—you still have to remember what those lists contain. Two, there's no existing evidence that English Catholics used songs or nursery rhymes as spiritual mnemonics (on the contrary, many nursery rhymes were political satires). Three, "The Twelve Days of Christmas" very likely originated in France: three French versions of the song exist, and at least one item in the song, the partridge, was known in France but wasn't introduced in England until the late 1770s. France, incidentally, has been Catholic for centuries.
The English version of the song first appeared in print in 1780, when it was listed as a "memory-and-forfeits" game in a children's book, Mirth Without Mischief. The object of the game was to sing along without mixing up any of the gifts; a mistake could cost a kiss or a candy. However, the song was certainly popular before 1780, and it might even be related to an older song called "A New Dial," or "In Those Twelve Days." "A New Dial" can be dated back to at least 1625 and was explicitly spiritual, though its purpose was not to teach a catechism. And while no evidence links the two songs, the "meaning" assigned to days two, four, six, eight, and ten is the same for both.