您只需加入两次Posteta:
global $wpdb, $bp;
$user_id = get_current_user_id();
$query = $wpdb->prepare(
"SELECT
rel.post_id as id,
rel2.meta_value as val
FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->postmeta} AS rel ON
posts.ID = rel.post_id
LEFT JOIN {$wpdb->postmeta} AS rel2 ON
posts.ID = rel2.post_id
WHERE
posts.post_type = \'books\' AND
posts.post_title LIKE \'%%fire\' AND
posts.post_status = \'publish\' AND
rel.meta_key = \'books_id\' AND
rel.meta_value = \'1351\' AND
rel2.meta_key = %d AND
rel2.meta_value > 0",
$user_id
);
$values = $wpdb->get_results( $query );
还请注意,我使用了
$wpdb->prepare()
代替
sprintf()
. 这是使用WordPress将值安全地放入查询的首选方法。