WordPress 中每位作者的最新帖子

发布于 2024-12-28 06:56:19 字数 688 浏览 4 评论 0原文

我有一个拥有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回眸一遍 2025-01-04 06:56:19

尝试将其替换

query_posts( array( 
    'post_type'         => 'post',
    'posts_per_page'    => 10,
    'post__in'          => $post_id
) ); 

为:

query_posts( array( 
    'post_type'         => 'post',
    'posts_per_page'    => 10,
    'post__in'          => $post_id,
    'orderby'           => 'date',
    'order'             => 'DESC'

) ); 

它将显示最新的帖子。

Try replacing this:

query_posts( array( 
    'post_type'         => 'post',
    'posts_per_page'    => 10,
    'post__in'          => $post_id
) ); 

with this:

query_posts( array( 
    'post_type'         => 'post',
    'posts_per_page'    => 10,
    'post__in'          => $post_id,
    'orderby'           => 'date',
    'order'             => 'DESC'

) ); 

It will show the newest posts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文