Automattic / Co-Authors-Plus

Multiple bylines and Guest Authors for WordPress
https://wordpress.org/plugins/co-authors-plus/
GNU General Public License v2.0
291 stars 204 forks source link

Functions that return posts by same author #709

Closed asscore closed 4 years ago

asscore commented 5 years ago

Hello, we have some weird use cases on a website, and we have a function that checks the author of the current post and returns a list of posts by the same author. It works fine by itself. We were hoping it would continue to work correctly after adding the Co-Authors-Plus plugin. However it seems to only associate the posts with the first author added. Any idea on how I can modify this function to work with the Co-Authors-Plus plugin?


function xxx_get_related_author_posts() {
    global $authordata, $post;
    $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) );
    if ( ! empty( $authors_posts ) ) {
        $output = '';
    }
    foreach ( $authors_posts as $authors_post ) {
        $output .= '<div><a href="' . get_permalink( $authors_post->ID ) . '" class="d-flex"><i class="far fa-newspaper"></i> ' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></div>';
    }
    return $output;
    wp_reset_postdata();
}```

Thanks
rebeccahum commented 4 years ago

Instead of using the global author data (which doesn't include guest authors), you may need to grab the coauthor data via get_coauthors(): https://github.com/Automattic/Co-Authors-Plus/blob/a944ef5961272ef9ad5b8c8310de7e2bcfca58c2/template-tags.php#L3