(+84) 463.28.7979

How to Display Related Posts by Same Author in WordPress


In the past, we have shown you how to display related WordPress posts by categories or tags without a plugin. We have also shown you how to display related posts with a WordPress plugin YARPP which has its own formula of determining which posts is related or not. Some of our users asked us if it was possible to display related posts by same author which we think is a pretty handy feature for multi-author blogs. So in this article, we will show you how to display related posts by the same author in WordPress without a plugin.

First, open your theme’s functions.php file and add the following code:
01 function get_related_author_posts() {
02 global $authordata, $post;
03
04 $authors_posts = get_posts( array( 'author' => $authordata->ID,'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) );
05
06 $output = '<ul>';
07 foreach ( $authors_posts as $authors_post ) {
08 $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>';
09 }
10 $output .= '</ul>';
11
12 return $output;
13 }

Then you need to open your single.php file (for twenty ten theme, loop-single.php), and paste the following code inside the loop where you like:

1 <?php echo get_related_author_posts(); ?>

The code above will basically display 5 recent posts by the same author, and it also make sure that there are no duplicates (i.e the current post will not be in the list). This is a very simple trick that does the trick without any hassles.

You can further customize the display by adding post thumbnails or other styling by editing the output lines of the function.



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>