我在使用自定义 Wordpress MySQL 查询时遇到问题。我的情况是这样的:我有用于帖子的常规帖子类型“帖子”和用于有关作者信息的自定义帖子类型“作者”。“作者”帖子类型包含所有作者,但并非所有人都需要是帖子的作者。
每个帖子(“帖子”和“作者”类型)都有一个带有作者确切姓名的自定义分类法(例如“John Smith”)。帖子类型“作者”具有作者的名字和姓氏的附加自定义元值(因为它们可能变得复杂并且更容易按姓氏、名字排序)。
现在我正在尝试选择所有已发布帖子的作者,计算他们的帖子数量并显示与他们的姓名相关联的元值。我不确定如何交叉引用来自一种帖子类型('post')的分类法与一行中的另一种('author')的元值。
我想要的是:
John Smith John Smith 10
到目前为止我得到了什么:
John Smith 10 John
John Smith 10 Smith
我坚持下面的查询。任何帮助都感激不尽!
SELECT
N, C, meta_value
FROM
(SELECT
t.name AS N, count(*) AS C
FROM
wp_2_posts p
INNER JOIN wp_2_term_relationships AS tr ON p.ID=tr.object_id
INNER JOIN wp_2_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN wp_2_terms AS t ON tt.term_id = t.term_id
WHERE 1=1
AND tt.taxonomy = 'myauthor'
AND p.post_type = 'post'
AND p.post_status = 'publish'
GROUP BY
N
ORDER BY
C DESC) AS x
INNER JOIN wp_2_posts p2
INNER JOIN wp_2_term_relationships AS tr2 ON p2.ID=tr2.object_id
INNER JOIN wp_2_term_taxonomy AS tt2 ON tr2.term_taxonomy_id = tt2.term_taxonomy_id
INNER JOIN wp_2_terms AS t2 ON tt2.term_id = t2.term_id
INNER JOIN wp_2_postmeta AS m2 ON m2.post_id = p2.ID
WHERE 1=1
AND post_type = 'author'
AND t2.name = x.N
AND (m2.meta_key = 'lastname' OR m2.meta_key = 'firstname')