Mesostructured and mesoporous MgAPOs and MgAPSOs were synthesised using cationic templating in the presence of various relative amounts of Mg2+ and the silica source. The products were characterised by X-ray diffraction, 27Al, 31P and 29Si solid-state NMR, thermal and elemental analyses and nitrogen adsorption. Magnesium is incorporated into the structure substituting for either the Al3+ units or the surfactant cations, with a consequent decrease of mesoscopic ordering. Incorporation of large amounts of magnesium in either mesostructured AlPO or SAPO gives products with significantly larger pore diameters than those of MgAPO and MgAPSO with lower Mg content, or AlPO or SAPO made using the same P2O5:Al2O3 ratio. Introduction of Si into the framework leads to an increased degree of condensation of the inorganic framework, and improves the thermal stability of the products.