On the stability conditions for theories of modified gravity in the presence of matter fields