It's not exactly a golden age for Turkish foreign policy.

ISIS is on Turkey's southern border. Ankara's having trouble handling its always fractious relationship with the Kurds. President Reccip Tayyip Erdogan can't seem to go a week without lashing out at his ostensible allies.

And today, Turkey failed in its bid for a two-year term on the UN Security Council, losing out to Spain and New Zealand in a vote before the UN General Assembly over two open seats.

The result comes as a surprise, since Spain is one of Europe's perennial economic trouble spots, and New Zealand is a geographically isolated island nation of 4.5 million people.

Turkey, on the other hand, fancies itself a rising superpower, a NATO member boasting the world's 17th-largest economy along with a sizable and advanced military. Its leaders have even tried to take a leading role in reforming the Security Council in a way that would reflect the ascendancy of emerging powers like Turkey.

Newsweek's Benny Avni was correct in calling today's vote "a tremendous upset." Avni noted that Turkey's foreign minister had hosted a party for diplomats at Manhattan's Waldorf-Astoria the night before the vote, "where many of the guests predicted an easy victory for Turkey."

But Turkey was apparently complacent and didn't realize how dead set its two biggest rivals in the Middle East were against letting Ankara hold one of 15 UNSC votes. Per Avni, the two countries most suspicious of the Muslim Brotherhood wanted Erdogan to pay a tangible diplomatic cost for his support for the group:

In the past few days, according to several diplomatic sources, there was an intense campaign, led by Egypt and Saudi Arabia, against Turkey’s membership in the council. The two countries are angered by President Recep Tayyip Erdoğan’s support for the Muslim Brotherhood, which both are fighting at home.

Another possible explanation is Turkey's disastrously muddled handling of its border with Syria. Turkey has lined tanks along the border near the besieged city of Kobane, but steadfastly declined to come to the city's aid as ISIS closed in — at the same time its officials were slamming the allied bombing missions against Islamic State fighters as a "PR campaign."

Turkey has taken a harder line against Syrian President Bashar Assad than just about any other NATO country, something that's led to the government turning a systemic blind eye toward jihadist recruiting within its own borders.

The vote not only denies Turkey one of the most prestigious and powerful positions in the entire international system — it's also a sign of how frustrated much of the world is with Ankara's trajectory, and how ineffective Iraq and Syria's northern neighbor now is at stating its case.