Wpdb按分类获得帖子SQL

时间:2016-04-20 作者:Web-Entwickler

我需要一个特定post\\u类型的帖子列表,外加一个自定义分类术语的名称。

这就是我目前得到的

SELECT p.post_name,t.name as clientName 
FROM $wpdb->posts AS p, $wpdb->terms AS t
LEFT JOIN $wpdb->term_relationships AS tr ON (\'p.ID\' = tr.object_id)
LEFT JOIN $wpdb->term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
LEFT JOIN $wpdb->terms AS t2 ON (t2.term_id = tt.term_id)
WHERE   p.post_status = \'publish\' 
    AND p.post_type = \'portfolio\'
    AND tt.taxonomy = \'clients\' 
ORDER BY p.post_date DESC
有什么问题?我得到了太多的结果,我得到了正确的帖子,但每个客户都有好几次。我想这是因为我使用$wpdb->术语作为t和t2?但当我尝试将其用作t时,两次都会出现错误

Not unique table/alias: \'t\'
谁能帮忙?

1 个回复
最合适的回答,由SO网友:Web-Entwickler 整理而成

抱歉,伙计们,我刚刚找到了解决方案:

SELECT p.post_name, t.name as clientName 
FROM $wpdb->posts AS p
INNER JOIN $wpdb->term_relationships AS tr ON (\'p.ID\' = tr.object_id)
INNER JOIN $wpdb->term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
INNER JOIN $wpdb->terms AS t ON (t.term_id = tt.term_id)
WHERE   p.post_status = \'publish\' 
    AND p.post_type = \'portfolio\'
    AND tt.taxonomy = \'clients\' 
ORDER BY p.post_date DESC
我不需要为定义别名$wpdb->terms 在里面FROM 条款