improved query for gallery page