It is narrated from al-Barāʾ ibn ʿĀzib (may Allah be pleased with him): “The Messenger of Allah ﷺ prayed facing Jerusalem for six—or, according to another report, seventeen—months, and the Messenger of Allah ﷺ wished …” and so on.
Al-Bukhārī recorded the report with this wording (1); it is likewise found in Muslim, al-Tirmidhī—who declared it authentic—al-Nasāʾī, Ibn Mājah, al-Ṭayālisī, and Aḥmad, with wording similar to his, though omitting the phrase, “and the foolish among the people …,” etc.
Al-Ḥāfiẓ Ibn Ḥajar, commenting in Fatḥ al-Bārī under the statement “and he loved to be directed toward the Kaʿbah,” writes:
“An explanation of this is supplied in what al-Ṭabarī and others transmitted through ʿAlī ibn Abī Ṭalḥah from Ibn ʿAbbās, who said: ‘When the Prophet ﷺ migrated to Madinah—and the Jews, who formed the majority of its inhabitants, faced Jerusalem—Allah commanded him to face Jerusalem. The Jews rejoiced, and he faced it for seventeen months. Yet the Messenger of Allah ﷺ desired to face the qiblah of Abraham; he would supplicate and look toward the sky, whereupon the verse was revealed. Another chain, from Mujāhid, states: He wished to turn toward the Kaʿbah only because the Jews said, “Muhammad opposes us yet follows our qiblah,” so the verse was revealed.’”
I say: Al-Ḥāfiẓ remained silent regarding this ḥadith of Ibn ʿAbbās, perhaps because its flaw is self-evident. ʿAlī ibn Abī Ṭalḥah, though trustworthy, was prone to error; he transmitted mursal reports from Ibn ʿAbbās without having met him, as al-Ḥāfiẓ himself notes in al-Taqrīb. Consequently, the ḥadith in this particular wording is weak on account of its broken chain.