Get authors who has posts in category

I’m using Barcelona theme and I need to get authors who have posted in certain category.

In my author.php template I have:

Read More
$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 );
}
?>

How to get authors who have posted to category ID 59?

For example I tried with:

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

But I’m getting error. Any help?

ERROR:

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

Related posts

2 comments

  1. Your get_posts( 'category=59' ) code should work. I have tested the following on one of my test wordpress installation and it works (with a different category ID of course). If you get an error during the get_posts call you need to show us the error.

    <?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...
        }
    
    ?>
    
  2. This was the solution for 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
    ) );
    

Comments are closed.