Now, it is not inappropriate if also, at times, the requests of some who pray are not granted by God.
 For we showed by reasoning that God fulfills the desires of a rational creature, to the extent that he desires the good. Now, it sometimes happens that what is sought in prayer is not a true, but an apparent, good; speaking absolutely, it is an evil. Therefore, such a prayer is not capable of being granted by God. Hence, it is said in James (4:3): “You ask and you receive not, because you ask amiss.”
 Likewise, because God moves us to the act of desiring, we showed that it is appropriate for Him to fulfill our desires. Now, the thing that is moved is not brought to its end by the mover unless the motion be continued. So, if the movement of desire is not continued by constant prayer, it is not inappropriate for the prayer to fail to receive its expected result. Hence, the Lord says in Luke (18:1) “that we ought always to pray and not to faint”; also, the Apostle says, in 1 Thessalonians (5:17): “Pray without ceasing.”
 Again, we showed that God fulfills in a suitable way the desire of a rational creature, depending on its nearness to Him. But one becomes near to Him through contemplation, devout affection, and humble but firm intention. So, the prayer which does not approach God in this way is not capable of being heard by God. Hence, it is said in the Psalm (101:18): “He has had regard to the prayer of the humble”; and in James (1:6): “Let him ask in faith, nothing wavering.”
 Besides, we showed that on the basis of friendship God grants the wishes of those who are holy. Therefore, he who turns away from God’s friendship is not worthy of having his prayer granted. Hence, it is said in Proverbs (28:9): “He who turns away his ears from hearing the law, his prayer shall be an abomination.” And again in Isaiah (1:15): “When you multiply prayer, I will not hear, for your hands are full of blood.”
 For the same reason it happens sometimes that a friend of God is not beard when he prays for those who are not God’s friends, according to the passage in Jeremiah (7:16): “Therefore, do not You pray for this people, nor take to You praise and supplication for them, and do not withstand me: for I will not hear You.”
 However, it happens at times that a person is refused because of friendship a petition which he asks of a friend, since he knows that it is harmful to him, or that the opposite is more helpful to him. Thus, a physician may deny sometimes the request of a sick person, having in mind that it is not beneficial to him in the recovery of his health. Consequently, since we showed that God, because of the love which He has for the rational creature, satisfies his desires when they are presented to Him through prayer, it is no cause for astonishment if at times He does not grant the petition, even of those whom He especially loves, in order to provide something that is more helpful for the salvation of the petitioner. For this reason, He did not withdraw the sting of the flesh from Paul, though he asked it thrice, for God foresaw that it was helpful to him for the preservation of humility, as is related in 2 Corinthians (12:7-9). Hence, the Lord says to certain people, in Matthew (20:22): “You do not know what you ask”; and it is said in Romans (8:26): “For we do not know what we should pray for as we ought.” For this reason, Augustine says in his letter to Paulinus and Therasia: “The Lord is good, for He often does not grant what we desire, so that He may give us what we desire even more.”
 It is apparent, then, from the foregoing that the cause of some things that are done by God is prayers and holy desires. But we showed above that divine providence does not exclude other causes; rather, it orders them so that the order which providence has determined within itself may be imposed on things. And thus, secondary causes are not incompatible with providence; instead, they carry out the effect of providence. In this way, then, prayers are efficacious before God, yet they do not destroy the immutable order of divine providence, because this individual request that is granted to a certain petitioner falls under the order of divine providence. So, it is the same thing to say that we should not pray in order to obtain something from God, because the order of His providence is immutable, as to say that we should not walk in order to get to a place, or eat in order to be nourished; all of which are clearly absurd.
 So, a double error concerning prayer is set aside as a result of the foregoing. Some people have said that prayer is not fruitful. In fact, this has been stated both by those who denied divine providence altogether, like the Epicureans, and by those who set human affairs apart from divine providence, as some Peripatetics do, and also by those who thought that all things subject to providence occur of necessity, as the Stoics did. From all these views, it follows that prayer is fruitless and, consequently, that all worship of the Deity is offered in vain. Indeed, this error is mentioned in Malachi (3:14), where it states: “You have said: He labors in vain who serves God. And what profit is it that we have kept His ordinances, and that we have walked sorrowful before the Lord of hosts?”
 Contrariwise, others have in fact said that the divine disposition is capable of being changed by prayers; thus, the Egyptians said that fate was subject to change by prayers and by means of certain idols, incensings, or incantations.
 Indeed, certain statements in the divine Scriptures seem, according to their superficial appearance, to favor this view. For it is said that Isaiah, at the command of the Lord, said to King Hezekiah: “Thus says the Lord: Take order with Your house, for You shall die, and shall not live”; and that, after the prayer of Hezekiah, “the word of the Lord came to Isaiah, saying: Go and say to Hezekiah... I have heard Your prayer... behold I will add to Your days fifteen years” (Is. 38:1-5). And again, it is said in the name of the Lord: “I will suddenly speak against a nation and against a kingdom to root out and to pull down and to destroy it. If that nation against which I have spoken shall repent of their evil, I will also repent of the evil that I have thought to do to them” (Jer. 18:7-8). And in Joel (2:13-14): “Turn to the Lord your God, for He is gracious and merciful... Who knows whether God will return and forgive?”
 Now, these texts, if understood superficially, lead to an unsuitable conclusion. For it follows, first of all, that God’s will is mutable; also, that something accrues to God in the course of time; and further, that certain things that occur in time to creatures are the cause of something occurring in God. Obviously, these things are impossible, as is evident from earlier explanations.
 They are opposed, too, by texts of Sacred Scripture which contain the infallible truth clearly expressed. Indeed, it is said in Numbers (23:19): “God is not as a man that He should lie, nor as the son of man that He should be changed. Did He say then, and will not do it? Has he spoken, and will He not fulfill?” And in 1 Sam (15:29): “The triumpher in Israel will not spare, and will not be moved to repentance; for He is not a man that He should repent.” And in Malachi (3:6): “I am the Lord and I do not change.”
 Now, if a person carefully considers these statements, he will find that every error that occurs on these points arises from the fact that thought is not given to the difference between universal and particular order. For, since all effects are mutually ordered, in the sense that they come together in one cause, it must be that, the more universal the cause is, the more general is the order. Hence, the order stemming from the universal cause which is God must embrace all things. So, nothing prevents some particular order from being changed, either by prayer, or by some other means, for there is something outside that order which could change it. For this reason, it is not astonishing for the Egyptians who reduce the order of human affairs to the celestial bodies, to claim that fate, which depends on the stars, can be changed by certain prayers and ceremonies. Indeed, apart from the celestial bodies and above them is God, Who is able to impede the celestial bodies' effect which was supposed to follow in things here below as a result of their influence.
But, outside the order that embraces all things, it is not possible for anything to be indicated by means of which the order depending on a universal cause might be changed. That is why the Stoics, who considered the reduction of the order of things to God to be to a universal cause of all things, claimed that the order established by God could not be changed for any reason. But, again on this point, they departed from the consideration of a universal order, because they claimed that prayers were of no use, as if they thought that the wills of men and their desires, from which prayers arise, are not included under that universal order. For, when they say that, whether prayers are offered or not, in any case the same effect in things follows from the universal order of things, they clearly isolate from that universal order the wishes of those who pray. For, if these prayers be included under that order, then certain effects will result by divine ordination by means of these prayers, just as they do by means of other causes. So, it will be the same thing to exclude the effect of prayer as to exclude the effect of all other causes. Because, if the immutability of the divine order does not take away their effects from other causes, neither does it remove the efficacy of prayers. Therefore, prayers retain their power; not that they can change the order of eternal control, but rather as they themselves exist under such order.
 But nothing prevents some particular order, due to an inferior cause, from being changed through the efficacy of prayers, under the operation of God Who transcends all causes, and thus is not confined under the necessity of any order of cause; on the contrary, all the necessity of the order of an inferior cause is confined under Him as being brought into being by Him. So, in so far as something in the order of inferior causes established by God is changed through prayer, God is said to turn or to repent; not in the sense that His eternal disposition is changed, but that some effect of His is changed. Hence, Gregory says that “God does not change His plan, though at times He may change His judgment”; not, I say, the judgment which expresses His eternal disposition, but the judgment which expresses the order of inferior causes, in accord with which Hezekiah was to have died, or a certain people were to have been punished for their sins. Now, such a change of judgment is called God’s repentance, using a metaphorical way of speaking, in the sense that God is disposed like one who repents, for whom it is proper to change what he had been doing. In the same way, He is also said, metaphorically, to become angry, in the sense that, by punishing, He produces the same effect as an angry person. Source