get query beforehand to avoid 'query is not defined'