我认为您使用自定义SQL是在开玩笑,其复杂性似乎仍在WP_Query
. 即使不是这样,也最好从它开始,为自定义SQL提供更坚实的基础。
如果我尝试这样的方法:
$meta_query = array(
array( \'key\' => \'body_type\', \'value\' => \'bt\' ),
array( \'key\' => \'Make\', \'value\' => \'make\' ),
array( \'key\' => \'Model\', \'value\' => \'model\' ),
array( \'key\' => \'Year\', \'value\' => array( 2010, 9999 ), \'compare\' => \'BETWEEN\' ),
);
$query = new WP_Query( array( \'meta_query\' => $meta_query ) );
$query->get_posts();
var_dump( $wpdb->queries );
这是生成的SQL:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id)
INNER JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id)
WHERE 1=1
AND wp_posts.post_type = \'post\'
AND (wp_posts.post_status = \'publish\'
OR wp_posts.post_status = \'private\')
AND ((wp_postmeta.meta_key = \'body_type\'
AND CAST(wp_postmeta.meta_value AS CHAR) = \'bt\')
AND (mt1.meta_key = \'Make\'
AND CAST(mt1.meta_value AS CHAR) = \'make\')
AND (mt2.meta_key = \'Model\'
AND CAST(mt2.meta_value AS CHAR) = \'model\')
AND (mt3.meta_key = \'Year\'
AND CAST(mt3.meta_value AS CHAR) BETWEEN \'2010\' AND \'9999\'))
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC LIMIT 0, 10
请注意
GROUP BY
最后,它可能会处理您正在获得的副本。
看见Custom Field Parameters 有关元查询的更多信息,请参阅Codex中的文档。