How To Display Related Posts by Author In WordPress

Post note

In a previous article you would of learnt How To Get Related Posts For WordPress Post. With a blog that has multiple authors you may want to display posts which are related to the current post but written by the same author.

This snippet will allow you to display related post by the same author.

To do this we need to add some code into both the functions.php page and the single.php page.

Functions.php

First add the following into your functions.php page.

function get_related_author_posts() {
    global $authordata, $post;
    $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 10 ) );
    $output = '<ul>';
    foreach ( $authors_posts as $authors_post ) {
        $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>';
    }
    $output .= '</ul>';
    return $output;
}

This will get posts by the current author where the post ID is not the current post and gets a maximum of 10 results.

Single.php

Now we have the function in the functions.php file we can use this inside the single.php file.

Place a call to this function where you want this list appear.

<?php echo get_related_athor_posts(); ?>

Advertise here

Comment