WordPress 中每位作者的最新帖子
我有一个拥有 28 个用户的 Wordpress 网站,并且希望从每个用户那里获取最新的帖子(每个用户只有一篇帖子)。我尝试过使用 DISTINCT 和 GROUP BY 来自定义 sql,但没有成功。
这是我距离最近的一次。它获取独特的帖子,但是最旧的帖子而不是最新的帖子。
function last_post_per_user() {
global $wpdb;
return $wpdb->get_results('SELECT ID FROM '.$wpdb->posts.' WHERE post_status=\'publish\' AND post_type=\'post\' AND post_author!=\'1\' GROUP BY post_author');
}
$posts = last_post_per_user();
foreach ($posts as $post) {
$post_id[] = $post->ID;
}
query_posts( array(
'post_type' => 'post',
'posts_per_page' => 10,
'post__in' => $post_id
) );
有人对如何解决这个问题有什么建议吗?
(实际上一整天都在尝试解决这个问题)
I have a Wordpress site with 28 users and would like to get the latest post from each user (just one post per user). I've tried with a custom sql using both DISTINCT and GROUP BY, but with no success.
This is the closest I have come. It fetches unique posts, but the oldest post instead of the newest.
function last_post_per_user() {
global $wpdb;
return $wpdb->get_results('SELECT ID FROM '.$wpdb->posts.' WHERE post_status=\'publish\' AND post_type=\'post\' AND post_author!=\'1\' GROUP BY post_author');
}
$posts = last_post_per_user();
foreach ($posts as $post) {
$post_id[] = $post->ID;
}
query_posts( array(
'post_type' => 'post',
'posts_per_page' => 10,
'post__in' => $post_id
) );
Does anyone have any suggestions on how to solve this?
(Have actually tried solving this the whole f*cking day)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将其替换
为:
它将显示最新的帖子。
Try replacing this:
with this:
It will show the newest posts.