Praise be to Allah.
Perhaps you are referring to the hadith which was narrated by at-Tirmidhi (346) and Ibn Majah (746) from Ibn ‘Umar (may Allah be pleased with him), which says that the Messenger of Allah (blessings and peace of Allah be upon him) forbade praying in seven places: garbage dumps, slaughterhouses, graveyards, the middle of the road, bathrooms, camel pens and on top of the Ka‘bah. But this hadith is da‘if (weak).
At- Tirmidhi said, after quoting this hadith: The isnad of Ibn ‘Umar’s hadith is not that strong.
It was also classed as da‘if by Abu Hatim ar-Raazi, as it says in al-‘Ilal by his son (1/148); by Ibn al-Jawzi in al-‘Ilal al-Mutanahiyah (1/399); al-Busiri in Misbah az-Zujajah (1/95); al-Hafiz in at-Talkhees (1/531-532); and al-Albani in al-Irwa’ (1/318).
Based on that, it is not valid to quote this da‘if hadith to prove that it is not allowed to pray in these places. But in the case of some of these places, it is proven that it is not allowed to pray in them, in other hadiths that are sahih (sound), such as the hadith narrated by Abu Dawud (492), at-Tirmidhi (317) and Ibn Majah (745) from Abu Sa‘id al-Khudri (may Allah be pleased with him), who said: The Messenger of Allah (blessings and peace of Allah be upon him) said: “All the earth is place of prostration, apart from graveyards and bathrooms.”
Shaykh al-Islam (may Allah have mercy on him) said: Its isnad is jayyid (good). Iqtida’ as-Sirat al-Mustaqim (p. 332); classed as sahih by al-Albani in al-Irwa’ (1/320).
Some of these places need to be discussed in detail:
1. Garbage dumps
This refers to places where garbage is thrown, and there may be impure things (najasah) in these places, so it is not allowed to pray in them, because of the impurities that they may contain.
Moreover, even assuming that they are pure, they are filthy places, so it is not appropriate for a Muslim to stand before Allah, may He be exalted, in such a place.
These are places in which animals are slaughtered. That is because these places are contaminated with impurities, such as blood and filth.
But if there is a clean and pure (tahir) space in the slaughterhouse, it is valid to pray there.
These are places where there are graves, and it is not allowed to pray in them, lest that lead to worshipping graves or resembling those who worship graves.
An exception is made in the case of the funeral prayer, which it is valid to offer in the graveyard. It is proven that the Prophet (blessings and peace of Allah be upon him) offered the funeral prayer for a woman who used to clean the mosque, after she had been buried in her grave. Narrated by al-Bukhari (460) and Muslim (956).
Another place in which it is not allowed to pray is a mosque that has been built over a grave, because of the mutawatir reports which say that the Prophet (blessings and peace of Allah be upon him) cursed those who take graves as places of worship, and he forbade doing that.
Shaykh al-Islam Ibn Taymiyah (may Allah have mercy on him) said:
These mosques that are built over the graves of prophets, righteous men, kings and others should be removed by being demolished or otherwise. This is a matter concerning which I know of no difference of opinion among the well-known scholars, and it is makruh to pray in such mosques, with no difference of scholarly opinion of which I am aware. In our view it is not valid [to pray in such mosques], which is also the favoured view of the madhhab, because of the prohibition and curse that have been narrated concerning that, and because of other hadiths."(Iqtida’ as-Sirat, p. 330).
4. The middle of the road
This refers to a road that is used by people; as for a disused road, or the side of the road where people do not walk or travel, it is not forbidden to pray there.
The reason for the prohibition on praying in the middle of the road is that it makes the way narrow and prevents people from passing, and it is a distraction to the worshipper himself, as he will be so distracted that he will be unable to pray properly.
Praying in the middle of the road is makruh (disliked) and may be haram (forbidden), if it harms people by preventing them from passing or the one who does that exposes himself to harm as a result of accidents and the like.
An exception from that is made in cases of need or necessity, such as Jumu‘ah prayer or Eid prayer in the street if the mosque is full. This is something that the Muslims often do.
This refers to places where people wash themselves, as is well known.
The prohibition on praying in bathrooms is proven in the hadith of Abu Sa‘id that was quoted above, which indicates that prayers offered in bathrooms are invalid.
The reason for the prohibition on praying in bathrooms is that they are places that are frequented by the devils and where ‘awrahs are uncovered.
The apparent meaning of the hadith indicates that the prohibition applies to any space that comes under the heading of a bathroom, so there is no difference between the place where a person bathes or a place where he takes off his clothes.
If it is forbidden to pray in bathrooms, then it is more appropriate that it should be forbidden to pray in toilets or lavatories (which are places where people relieve themselves). The only reason why there is no specific prohibition on praying in toilets is the fact that any rational person who hears that the Prophet (blessings and peace of Allah be upon him) forbade praying in bathrooms will realise that this prohibition applies even more so to praying in toilets.
Hence Shaykh al-Islam Ibn Taymiyah (may Allah have mercy on him) said:
There is no specific text regarding toilets [meaning the prohibition on praying in such places], because the matter was too clear to the Muslims to require any evidence."(Majmu‘ al-Fatawa 25/240).
6. Camel pens
This refers to places in which camels shelter (barns) and also the places in which they gather after having drunk water.
The reason for this prohibition is that camel pens are places where devils reside, and if the camels are there, they will distract the worshipper and prevent him from focusing properly, because he will be worried about them bothering him.
7. On top of the Ka‘bah
The scholars who regard that as disallowed say that it is because, in this case, the worshipper will not be facing in the direction of the qiblah; rather he will only be facing part of it, because part of the Ka‘bah will be behind his back.
Other scholars are of the view that it is valid to pray on top of the Ka‘bah, because it is proven that the Prophet (blessings and peace of Allah be upon him) prayed inside it in the year of the conquest [of Makkah], and praying on top of it is like that. In fact, praying on top of the Ka‘bah is not easy nowadays.
Another place in which it is also not allowed to pray is:
8. Land that has been seized by force
If someone has seized land by force, it is prohibited for him to pray there, according to scholarly consensus.
An-Nawawi (may Allah have mercy on him) said in al-Majmu‘ (3/169):
Praying in land that has been seized by force is prohibited according to scholarly consensus. End quote.
See also: ash-Sharh al-Mumti‘ (2/237-260); Sharh Bulugh al-Maram by Ibn ‘Uthaymin (1/518-522); Hashiyat Ibn Qasim (1/537-547).
And Allah knows best.