As for you, do not pray for these people, do not offer a plea or petition on their behalf, and do not beg Me, for I will not listen to you.

Jer 7:16

In addition to the time above, Jeremiah was told two other times, not to petition for his people (Jer 11:14; Jer 14:11). The reason? The Lord had already entered into judgment with them.

Do you not see what they are doing in the cities of Judah and in the streets of Jerusalem? The sons gather wood, the fathers light the fire, and the women knead the dough to make cakes for the Queen of Heaven; they pour out drink offerings to other gods to provoke Me to anger. But am I the One they are provoking? declares the LORD. Is it not themselves they spite, to their own shame?

Therefore this is what the Lord GOD says: Behold, My anger and My fury will be poured out on this place, on man and beast, on the trees of the field and the produce of the land, and it will burn and not be extinguished.

Jer 7:17-20

The Lord Almighty, who planted you, has decreed disaster for you, because the people of both Israel and Judah have done evil and aroused my anger...

Jer 11:17

This is what the LORD says about this people:

“Truly they love to wander;
     they have not restrained their feet.
So the LORD does not accept them;
     He will now remember their guilt
     and call their sins to account.”

Then the LORD said to me, “Do not pray for the well-being of this people. Although they may fast, I will not listen to their cry; although they may offer burnt offerings and grain offerings, I will not accept them. Instead, I will finish them off by sword and famine and plague.”

Jer 14:10-11

Notice, that in the statements above, Jeremiah was not commanded to not pray. But rather to not pray against what God had already determined.

We see an example of this again in the Book of Matthew. Jesus is describing how those ought to pray during the "last days" trouble:

Pray that your flight will not occur in the winter or on the Sabbath. For at that time there will be great tribulation, unmatched from the beginning of the world until now...

Matt 24:20

Notice that He didn't encourage them to pray that the trouble would stop. Remember, that during the "last days", it is Jesus that opens the seals.

And when the Lamb opened the second seal, I heard the second living creature say, “Come!”

Then another horse went forth. It was bright red, and its rider was granted permission to take away peace from the earth and to make men slay one another. And he was given a great sword.

Rev 6:3-4

Note Paul's instruction in the Book of Romans about how the "Holy Spirit bears witness with our spirit" (Rom 8:16).

In the same way, the Spirit helps us in our weakness. For we do not know how we ought to pray, but the Spirit Himself intercedes for us with groans too deep for words. And He who searches our hearts knows the mind of the Spirit, because the Spirit intercedes for the saints according to the will of God.

Rom 8:26-27

I believe that taking the time to stop and consider what the Holy Spirit is saying to our spirit is the beginning of learning to pray "according to the will of God".

Our Father in heaven,
     hallowed be Your name.
Your kingdom come,
     Your will be done...

Matt 6:9-10