【问题标题】:Latest post from each author in wordpresswordpress 中每位作者的最新帖子
【发布时间】:2012-01-20 20:47:10
【问题描述】:

我有一个拥有 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
) ); 

有人对如何解决这个问题有任何建议吗?

(实际上整整一天都在尝试解决这个问题)

【问题讨论】:

    标签: mysql wordpress group-by distinct


    【解决方案1】:

    尝试替换这个:

    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'
    
    ) ); 
    

    它将显示最新的帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 2010-11-24
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多