The explicit expressions for the average number of twisted photons radiated by a charged particle in an elliptical undulator in the classical approximation as well as in the approach accounting for the quantum recoil are obtained. It is shown that radiation emitted by a particle moving along an elliptical helix which evolves around the axis specifying the angular momentum of twisted photons obeys the selection rule: $m+n$ is an even number, where $m$ is a projection of the total angular momentum of a twisted photon and $n$ is the harmonic number of the undulator radiation. This selection rule is a generalization of the previously known selection rules for radiation of twisted photons by circular and planar undulators and it holds for both classical and quantum approaches. The class of trajectories of charged particles that produce the twisted photon radiation obeying the aforementioned selection rule is described.