获取在类别中有帖子的作者

Get authors who has posts in category

我正在使用 Barcelona theme,我需要找到在特定类别中发帖的作者。

在我的 author.php 模板中,我有:

$barcelona_authors = get_users( array(
    'fields' => 'ID',
    'who'    => 'authors',
    'order'  => 'DESC',
    'orderby'=> 'post_count'
) );

<?php
foreach ( $barcelona_authors as $barcelona_author_id ) { 
  barcelona_author_box( $barcelona_author_id, false );
}
?>

如何获取发布到类别 ID 59 的作者?

例如我试过:

$barcelona_authors = get_posts('category=59');

但是我遇到了错误。有帮助吗?

错误:

Notice: Object of class WP_Post could not be converted to int in /home/wp-includes/author-template.php on line 296

您的 get_posts( 'category=59' ) 代码应该可以工作。我已经在我的一个测试 wordpress 安装上测试了以下内容并且它可以工作(当然使用不同的类别 ID)。如果您在 get_posts 调用期间遇到错误,您需要向我们显示错误。

<?php

    $category_posts = get_posts( 'category=59' );
    $authors_ids = array();
    $authors = array();

    foreach( $category_posts as $cat_post ) {
        $authors_ids[] = $cat_post->post_author;
    }

    // Not sure if you need more data than just the ID so here is what you need
    // to get other fields.

    foreach( $authors_ids as $id ) {
        $user = get_user_data( $id );
        // Display name: $user->display_name;
        // Nice name: $user->user_nicename;
        // etc...
    }

?>

这是 Barcelona theme 的解决方案:

$barcelona_posts = get_posts('cat=59');
$barcelona_author_ids = array();

foreach ( $barcelona_posts as $k => $v ) {
    if ( ! in_array( $v->post_author, $barcelona_author_ids ) ) {
        $barcelona_author_ids[] = $v->post_author;
    }
}

$barcelona_authors = get_users( array(
    'fields' => 'ID',
    'who'    => 'authors',
    'order'  => 'DESC',
    'orderby'=> 'post_count',
    'include' => $barcelona_author_ids
) );