All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad, sallallaahu ‘alayhi wa sallam, is His Slave and Messenger.
Allaah The Almighty forbade His Messenger and the believers to seek forgiveness for non-Muslims in the verse in which Allaah The Almighty Says (what means): {It is not for the Prophet and those who have believed to ask forgiveness for the polytheists, even if they were relatives, after it has become clear to them that they are companions of Hellfire.} [Quran 9:113] This verse was revealed when the Messenger of Allaah, sallallaahu ‘alayhi wa sallam, said regarding Abu Taalib: I will invoke Allaah for forgiveness for you, as long as I am not prohibited from doing so. Then all the Muslims began to seek forgiveness for their dead relatives.
This verse became clear-cut evidence that forbids seeking forgiveness for non-Muslims. As for offering the funeral prayer for ‘Abdullaah ibn Ubay, the Prophet, sallallaahu ‘alayhi wa sallam, did that because Ubayy pretended to be a Muslim. Imaam Al-Qurtubi said in his Tafseer: “Some scholars said, the Prophet, sallallaahu ‘alayhi wa sallam, offered the funeral prayer for ‘Abdullaah ibn Ubayy based on his verbal pronouncement of Islam. Then he gave up doing that when he was prohibited to do so.”
Al-Haafith Ibn Hajar said: “‘Umar was absolutely certain that he (Ubayy) was a hypocrite because what he (‘Umar) witnessed from him. The Prophet, sallallaahu ‘alayhi wa sallam, did not act according to the opinion of ‘Umar because Ubayy had declared Islam outwardly. Furthermore, it was a form of honoring his son (Ubayy’s son) whose truthfulness and loyalty had been proved, and a way of seeking the friendship of his people and warding off their harm.
Accordingly, there is no contradiction between the two issues as we have explained.
Your second question is related to the issue of burying more than one person in the same grave. The exact wording of the Hadeeth is: “And put the one with the most Quran first.” [Abu Daawood, At-Tirmithi and An-Nasaa’i]
This means putting the one who memorized more Quran first, which is proof of his merit. Al-Mubaarakfoori, in Sharh At-Tirmithi, said: “Put the one with the most Quran closer to the wall of the grave to be closer to the Ka‘bah. This indicates revering the person who is honored for his knowledge and deeds in his life and death.”
All the narrations of the Hadeeth have the same meaning that indicates putting those who memorized more of the Quran in front of others in a common grave.
Allaah Knows best.