It's not the entire purpose of the loyalty program. The other reason (possibly the bigger reason) is simple price discrimination. McDonald's found that there are two distinct groups of customers: habitual fast food eaters, who go to their restaurants on a very regular basis, and opportunistic fast food eaters, who grab some fast food once in a blue moon because they want something fast.
They found, perhaps counterintuitively, that the opportunistic customers are vastly less price sensitive than the habitual customers. The solution was a somewhat involved loyalty program, which is deliberately enough effort to set up to weed out the sporadic customers from using it. Now, they can charge the less-price sensitive one price, and their more price sensitive customers a different, lower price. As an added bonus, habitual customers are probably more susceptible to advertising/prompting.