Break post and postmeta SELECT into two queries

The postmeta table can contain duplicates since there is no
UNIQUE constraint on the (post_id, meta_key) pair, like there
is in other wordpress tables, such as wp_options. This causes
the LEFT JOIN on postmeta to select posts multiple times when
a duplicate meta_key exists. Breaking the query into two
allows us to ignore duplicate meta keys.
Properly escape string meta/option keys