Select certain terms in an expression
expr = a q[i] + b + c q[j] + d;
f = DeleteCases[#, Except[_. _q], If[Head[#] === q, 0, 1]] &
f @ expr
a q[i] + c q[j]
f @ q[i]
q[i]
Alternatively,
f2 = Select[#, Function[x, If[Head@# === q, True, Not @ FreeQ[x, _q]]]] &;
f2 @ expr
a q[i] + c q[j]
f2 @ q[i]
q[i]
A pattern-based approach.
fn[x_. y_q + z_.] := x y + fn[z]
_fn = 0;
a q[i] + b + c q[j] + d // fn
q[i] // fn
a q[i] + c q[j] q[i]