$post = $wpdb->get_results("SELECT `p`.`ID`, MAX(p.post_date) as `datetime`, `p`.`post_date`, `p`.`post_content`, `p`.`post_title`, `p`.`post_status`, `p`.`post_name`, `p`.`comment_count`, `tax`.`term_taxonomy_id`, `tax`.`term_id`, `tax`.`taxonomy`, `tax`.`parent`, `rel`.`object_id`, `rel`.`term_taxonomy_id`, `t`.`term_id`, `t`.`name`, `t`.`slug`
FROM (`$wpdb->posts` AS p)
INNER JOIN `$wpdb->term_relationships` AS rel ON `rel`.`object_id` = `p`.`ID`
INNER JOIN `$wpdb->term_taxonomy` AS tax ON `tax`.`term_taxonomy_id` = `rel`.`term_taxonomy_id`
INNER JOIN `$wpdb->terms` AS t ON `t`.`term_id` = `tax`.`term_id`
WHERE `tax`.`taxonomy` = 'category'
AND `p`.`post_status` = 'publish'
AND `p`.`post_type` = 'post'
GROUP BY tax.parent
ORDER BY datetime DESC
LIMIT 4");
我需要找到每个类别的最新帖子,然后对结果进行分组,因此每个类别只有一个最新帖子。
我用; GROUP BY tax.parent
不工作;ORDER BY datetime DESC