The first point to make is to compare the account of Mary, Joseph and Jesus travels and the 40 years of Israel in the wilderness is a misunderstanding and conflation of the two accounts.
Herod’s Massacre of the Innocents was carried out, but his plan to rid the world of the Messiah was thwarted when God intervened to protect Jesus in fulfillment of prophecy. When the Magi left Bethlehem to return to their homeland, Joseph had a dream:
“And when they were departed, behold, the angel of the Lord appeareth to Joseph in a dream, saying, Arise, and take the young child and his mother, and flee into Egypt, and be thou there until I bring thee word: for Herod will seek the young child to destroy him. When he arose, he took the young child and his mother by night, and departed into Egypt: And was there until the death of Herod: that it might be fulfilled which was spoken of the Lord by the prophet, saying, Out of Egypt have I called my son.” Matthew 2:13–15
“When Israel was a child, then I loved him, and called my son out of Egypt.” Hosea 11:1
“Wilderness wandering” refers to the plight of the Israelites due to their disobedience and unbelief. Nearly 3,500 years ago, the Lord delivered His people from Egyptian bondage as described in Exodus, chapters 1–12. They were to take possession of the land God had promised their forefathers, a land “flowing with milk and honey”. Exodus 3:8
Prior to entry, however, they became convinced they could not oust the current inhabitants of the land who were giants (Numbers 13:27–33), even though God told them they could. Their lack of belief in God’s word and promises brought forth the wrath of God. He cursed them with forty years of wilderness wandering until the unbelieving generation died off, never stepping foot in the Promised Land.
A seven-year famine was responsible for God’s chosen people ending up in Egypt. Initially, they flourished under the leadership of Joseph, number two in charge of the country after Pharaoh. “Now there arose up a new king over Egypt, which knew not Joseph.” Exodus 1:8, and soon,
“But the more they afflicted them, the more they multiplied and grew. And they were grieved because of the children of Israel.” Exodus 1:12
For the next several centuries the Israelites were enslaved by the Egyptians who “worked them ruthlessly” Exodus 1:13
Eventually, God heard their cries (Exodus 2:23-25) and sent Moses and Aaron to rescue them. After enduring the last of the ten plagues—the death of the firstborn males—Pharaoh finally agreed to release the Israelites.
Upon their arrival at Kadesh Barnea, which bordered the Promised Land of Canaan, they sent out twelve spies to survey the land and its people (Numbers 13:18-25). They returned after forty days of exploration. Ten of the spies had a bad report: (Numbers 13:31-33). Only Joshua and Caleb dissented (Numbers 14:6-7). Believing the report of the ten doubters, the people lost heart and rebelled.
They raised their voices and wept aloud, grumbling against Moses and Aaron. “And all the congregation lifted up their voice, and cried; and the people wept that night. And all the children of Israel murmured against Moses and against Aaron: and the whole congregation said unto them, Would God that we had died in the land of Egypt! or would God we had died in this wilderness!” Numbers 14:1-2
“And the Lord said unto Moses, How long will this people provoke me? and how long will it be ere they believe me, for all the signs which I have shewed among them?” Numbers 14:11
However, Moses once again interceded for his people and turned away the wrath of God (Numbers 14:13-20). Although God did forgive them, He decided that “Surely they shall not see the land which I sware unto their fathers, neither shall any of them that provoked me see it:” Numbers 14:23
Rather, they would suffer by wandering in the wilderness for forty years, one year for each of the forty days they explored the land (Numbers 14:34). Furthermore, God would give them what they asked for:
“Say unto them, As truly as I live, saith the Lord, as ye have spoken in mine ears, so will I do to you: Your carcases shall fall in this wilderness; and all that were numbered of you, according to your whole number, from twenty years old and upward which have murmured against me.” Numbers 14:28-29
Additionally, the ten men who had given the bad report were struck down and died of a plague before the Lord (Numbers 14:37). Only Joshua and Caleb survived, the two faithful spies who believed God’s promise to give the land over to them.
God had promised them victory. The land He commanded them to go in and take was already theirs; they simply had to trust and obey, but this they did not do. God will never lead us where His grace cannot provide for us or His power cannot protect us. The Israelites had seen the powerful hand of God at work during the plagues and miracles of the Exodus.
Yet, like many people, they walked by sight and not by faith, and their unbelief displeased God. “Without faith it is impossible to please God” Hebrews 11:6. Their failure to believe in God’s word kept them from entering the Promised Land. This truth has never changed.