This ruling is based on a Hadith where the Prophet ﷺ mentioned that a person should not pray when he is fending off the call to nature, and this includes the need to pass wind.
عَنْ أَبِي هُرَيْرَةَ، قَالَ قَالَ رَسُولُ اللَّهِ ـ صلى الله عليه وسلم ـ “لاَ يَقُومُ أَحَدُكُمْ إِلَى الصَّلاَةِ وَبِهِ أَذًى”.
Abu Hurairah said, ‘The Messenger of Allah ﷺ said, ‘No one of you should stand to pray when he feels some discomfort (because of needing to urinate or defecate).‘ (Ibn Maja 618, Hasan).
The Hanafis consider holding back the need to relive oneself to be Mukruh Tahriman (prohibitively disliked). However, the dislike can drop in a Makruh act if there is a need or a necessity present.
If it means that a person is going to miss the entire prayer time, then for this person to hold back the need to relieve himself, and to just go ahead and pray is required. Not doing so, will lead to something worse, namely missing the prayer from its time.
If the concern is not the prayer time elapsing, rather it is causing great hardship for the person. Such that he is going to have to keep repeating Wudu, then this may be considered a need (Haajah), and holding back the urge to pass wind, in such a case, is not going to be Mukruh. Rather he may hold it back and pray.
(قَوْلُهُ وَصَلَاتُهُ مَعَ مُدَافَعَةِ الْأَخْبَثَيْنِ إلَخْ) أَيْ الْبَوْلِ وَالْغَائِطِ. قَالَ فِي الْخَزَائِنِ: سَوَاءٌ كَانَ بَعْدَ شُرُوعِهِ أَوْ قَبْلَهُ، فَإِنْ شَغَلَهُ قَطَعَهَا إنْ لَمْ يَخَفْ فَوْتَ الْوَقْتِ، وَإِنْ أَتَمَّهَا أَثِمَ لِمَا رَوَاهُ أَبُو دَاوُد «لَا يَحِلُّ لِأَحَدٍ يُؤْمِنُ بِاَللَّهِ وَالْيَوْمِ الْآخِرِ أَنْ يُصَلِّيَ وَهُوَ حَاقِنٌ حَتَّى يَتَخَفَّفَ»، أَيْ مُدَافِعُ الْبَوْلِ، وَمِثْلُهُ الْحَاقِبُ: أَيْ مُدَافِعُ الْغَائِطِ وَالْحَازِقُ: أَيْ مُدَافِعُهُمَا وَقِيلَ مُدَافِعُ الرِّيحِ اهـ. وَمَا ذَكَرَهُ مِنْ الْإِثْمِ صَرَّحَ بِهِ فِي شَرْحِ الْمُنْيَةِ وَقَالَ لِأَدَائِهَا مَعَ الْكَرَاهَةِ التَّحْرِيمِيَّةِ.
(رد المحتار على الدر المختار)
(His words, and his Salah while holding back the need to relieve himself) meaning urine and faeces. He said in al-Khazaain, this is irrespective of it occurring after beginning (the prayer) or before it. If it disturbs him, he breaks the Salah if he does not fear the prayer time elapsing. If he completes the prayer then he is sinful as narrated by Abu Dawood. “it is not permissible for none who believes in Allah and the last day to pray and he is Haaqin until he receives himself.” Meaning holding back urine, it is similar to al-Haaqib, which means holding back faeces. And al-Haaziq meaning holding them both back, another opinion says it is said holding back wind. The sin that has been mentioned, was explicitly clarified in the commentary of al-Munyah, he said the prayer is valid but Makruh Tahreemi (prohibitively disliked). (Ibn Abideen, Radd al-Muhtaar).
ومدافعا لأحد الأخبثين أو الريح (مراقي الفلاح)
And (it is Makruh) to pray holding back one of the tow filths (urine or faeces) or (holding back) wind. (Imam Shurunbulali, Maraqi al-Falah)
And Allah (Most High) knows best.
Answered by Shaykh Noor ud-deen (04.01.2021)