As @StephenHarris@StephenHarris pointed out in the comments, there's a core function that might come in handy when building the SQL Query: get_meta_sql()
- Examples in Codex. Basically this function is just used to build the meta SQL statement that gets used in WP_Query
, but you can use it in this case (or others) as well. The argument that you throw into it is an array, the exact same that would add to a WP_Query
.