我有一些常规的WordPress帖子,我需要通过父类别从数据库中获取,并按照我在数据库中自定义表的结果排序。
我想为(包括儿童)检索帖子的父类别称为资源管理器,其ID为29。
自定义表名为wp_upvotes
。在此表中有几列,但我们关注的唯一列可能是id
和postID
。我想按wp_posts
等于postID
的行数排序wp_post.ID
,如果该表中没有其他帖子的行,那么它们应该是按日期排序。我希望按日期upvotes
到upvotes
的最小数量{/ 1}}。
我试过的查询就是这个(它只返回第一篇文章,而不是全部文章):
$catIDs = array(29,30,31,32);
SELECT wp_posts.*, COUNT(wp_upvotes.id) AS upvotes FROM wp_posts
LEFT JOIN wp_upvotes ON (wp_posts.ID = wp_upvotes.postID)
INNER JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy
ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id IN (" . implode(',', $catIDs) . "))
AND wp_posts.post_status = 'publish'
ORDER BY upvotes DESC, wp_posts.post_date DESC
当我删除wp_upvotes
表的LEFT JOIN时,它会返回所有正确的帖子。为什么我在使用LEFT JOIN时只返回一行?
SELECT wp_posts.* FROM wp_posts
INNER JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy
ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id IN (" . implode(',', $catIDs) . "))
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date DESC
答案 0 :(得分:0)
看起来主要因素是使用ORDER BY wp_posts.ID
无论出于何种原因使其显示所有行而不只是一个..奇怪但这是我最后的代码,我想做我上面要做的事情:
SELECT wp_posts.*, COUNT(wp_upvotes.id) AS upvotes FROM wp_posts
INNER JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy
ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.parent = 29)
LEFT JOIN wp_upvotes ON (wp_posts.ID = wp_upvotes.postID)
WHERE wp_posts.post_status = 'publish'
GROUP BY wp_posts.ID
ORDER BY upvotes DESC, wp_posts.post_date DESC