SQL并没有将术语匹配仅限于类别。您还可以获得标签和任何其他分类法。因此,返回的数据可能比预期的要多。
Taxonomy是term\\u Taxonomy表中的一个键,因此消除不需要的分类应该很快。
SELECT DISTINCT(terms.term_id) as ID, terms.name
FROM $wpdb->posts as posts
LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
LEFT JOIN $wpdb->term_taxonomy as tax ON (relationships.term_taxonomy_id = tax.term_taxonomy_id AND tax.taxonomy = "category")
LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
WHERE posts.post_author = {$post->post_author}
可能会有帮助。您的DISTINCT还可能导致使用临时表进行排序等。您可以尝试使用GROUP BY,如下所示:
SELECT terms.term_id AS ID, terms.name
FROM $wpdb->posts AS posts
LEFT JOIN $wpdb->term_relationships AS relationships ON posts.ID = relationships.object_ID
LEFT JOIN $wpdb->term_taxonomy AS tax ON ( relationships.term_taxonomy_id = tax.term_taxonomy_id
AND tax.taxonomy = "category" )
LEFT JOIN $wpdb->terms AS terms ON tax.term_id = terms.term_id
WHERE posts.post_author = {$post->post_author}
AND terms.term_id IS NOT NULL
GROUP BY terms.term_id
可能会得到更好的结果。把它们都设置好,让它们通过phpMyAdmin运行,并让它对它们进行解释,看看哪一个能提供更好的结果。
此外,post\\u author是一个int。您不需要在它周围加引号。