1

这是我对 WP 数据库的查询。

$letters = $wpdb->get_col(
"SELECT DISTINCT LEFT(post_title,1) AS first_letter FROM $wpdb->posts
WHERE post_type = '$post_type' AND post_status = 'publish'
ORDER BY first_letter ASC"
);

这是一个婴儿姓名数据库,我想制作一个小部件来分隔男性/女性,所以我需要像上面一样进行查询,但要获取只有某些 postmeta 的帖子,下面是我正在寻找的关键和价值。

键 - spin2

价值 - 男性

由于它位于不同的表中,如何将其添加到查询中?

谢谢!

4

1 回答 1

1

你只需要使用一个INNER JOIN

SELECT DISTINCT LEFT(posts.post_title,1) AS first_letter 
FROM $wpdb->posts AS posts 
    INNER JOIN $wpdb->postmeta AS meta
        ON posts.ID = meta.post_id
WHERE posts.post_type = '$post_type' 
    AND posts.post_status = 'publish'
    AND meta.meta_key = 'spin2'
    AND meta.meta_value = 'Male'
ORDER BY first_letter ASC
于 2013-03-19T17:53:42.433 回答