use this as a starter
(Cos[27 W t] + 3 Cos[W t] + Cos[15 W t] + 4 Cos[28 W t]) /.
Cos[x_ W t] :> 0 /; Not[MemberQ[{1, 29, 15, 31}, x]]
You were counting on the fact that Mathematica puts the numbers first and only then the symbols. So, to be more general, check that x is actually a number
(Cos[27 W t] + 3 Cos[W t] + Cos[15 W t] + 4 Cos[28 W t]) /.
Cos[x_ y_] :> 0 /; NumberQ[x] && Not[MemberQ[{1, 29, 15, 31}, x]]
or you can check that x is a number inside the expression
(Cos[27 W t] + 3 Cos[W t] + Cos[15 W t] + 4 Cos[28 W t]) /.
Cos[x_?NumberQ y_] :> 0 /; Not[MemberQ[{1, 29, 15, 31}, x]]
(The ? is called PatternTest) and the /; is called condition.