Optimized get_tagged method

Removed unneeded code.
Only use one query and no more silly array iterations.
Merge pull request #2677 from ryun/patch-5

Optimized get_tagged method