King Sancho I had captured Silves in Algarve in 1189, however the great Muslim city was retaken by the Almohads in 1191 and all Portuguese conquests south of the Tagus river lost, with the exception of Évora, which remained in the hands of the Order of Aviz.
After this setback, the Portuguese nobility gradually abstained from further participation in the recapture of Muslim-held territory back to Christian dominion, preferring instead to leave such initiatives to the Catholic military Orders and the almogavars.
Having established himself at Niebla in 1234 or 1236, he took on the title of Emir of the West (Amir al-Gharb) and declared himself a vassal of Ferdinand III of Castile, in an effort to protect himself both from the Portuguese who advanced from the north as well as the Muslims who threatened him from Seville.
[1] From this point onwards, the Emirate of Niebla saw its land communications severed in half, but at this moment conflict sparked between Sancho and the Catholic Church, which would force the Order of Santiago to undertake the conquest of Algarve entirely on its own, without the support of the army or the fleet of the Portuguese king.
[1] Despite the acquisition of Estômbar and Alvor, the castles of Marrachique, Ourique, Messines and Montagudo defending the pathways south through the mountains were still in Muslim hands, making it difficult for the knights of Santiago to obtain fresh supplies from their bases further north, such as Aljustrel.
Ibn Mahfuz on his part lacked the means to properly defend western Algarve from the incursions of the knights, hence he offered to swap the castles of Estômbar and Alvor for Cacela Velha.
Ibn Mahfuz however,was aware that the lands around Cacela Velha were far less populated, while nearby Tavira was less vulnerable than Silves and the local terrain was flatter, thereby favouring the Muslim numerical advantage.
[1] On June 1239 most likely, Correia attempted a surprise attack against the Castle of Paderne, which defended a road connecting eastern Algarve to Silves, and from which raids could be launched in every surrounding direction.
[1] Having failed to decisively defeat the Christians on an open field engagement, the Muslims instead proposed a truce for the summer months so as to resume their harvest and pick fruit at peace, which Correia accepted so he could in turn replenish his forces and recruit more men.
[1] After Paio Peres Correia returned to Castile, the cities of Aljezur, Faro, Loulé, Porches and Albufeira still remained in Muslim hands, however they had declared vassalage to the Marinids of Morocco and were hard to take without the support of a fleet.
Once Afonso III emerged victorious from the civil-war that pitted himself against his brother in 1248, he undertook new attacks in the Algarve so as to seal his authority as king before the Portuguese nobility, to reward loyal followers and to fulfill a vow he had made to Pope Innocent IV in 1245 should he ascend to the throne.
This caused a diplomatic dispute between Portugal and Castile that was only definitively resolved with the signing of the Treaty of Badajoz in 1267, in which the rights of Afonso III were recognized and the Portuguese-Castilian border fixed on the Guadiana river.