Display Related Posts Based on Current Post’s Author

  • iftekhar
  • Apr 2, 2015
  • 0 comment
  • 1520 Views
  • Print

Just only couple of days back I wrote a post on How to Display Random Posts in WordPress and last year in December I wrote another post with very similar concept, which was How to Show Related Posts Based on Multiple Categories in WordPress. If you take a look at these posts carefully, you will find that I basically played around with the “Related Post” concept for WordPress platform, which I think is a great way to let your visitor explore your site or blog.

Again today, I will talk about the same concept but from a different angle. I will share a snippet code with you which will let you show the “Related Posts” underneath every single post of your blog based on the author. What I meant is, say for an example I am writing this post right now but when you would be reading this post, our snippet should display few other posts which were written by me. This could be a really cool idea especially if you have multi-author based site or blog. WordPress is a very powerful yet open source CMS (Content Management System) for any web site and it comes with tons of great features. All you have to do is just to dig little more inside. Here we go.

Here is the snippet that you may have been looking for:

<?php 
global $authordata, $post;
$authors_posts = get_posts( array(
   'author' => $authordata->ID,
   'post__not_in' => array($post->ID),
   'posts_per_page' => 5 ) );
echo '<div id="authors-post">';   
echo '<ul>';
foreach ($authors_posts as $post)  {
   echo '<li><a href="'.get_permalink($post->ID).'">';
   echo get_the_title().'</a></li>'; }
echo '</ul>';
echo '</div>';
wp_reset_postdata(); 
?>

Simply copy and paste the above snippet on your single.php page and update it. You are pretty much done. All you need to do is to style the div element with your CSS code.

Custom Post Type

If you have any custom post type registered on your site, this snippet can also be used to display list of custom posts written by the same author. The idea is pretty much the same like before but only thing that is changing here is the post type. In this case we just need mention or declare the post type in question. Simply add the following line inside your get_posts query.

'post_type' => 'CPT_name',

Rest of the snippet should remain the same. This snippet should work on both the single.php or single-cptname.php page.

Explanation

The ideas behind this snippet is fairly easy. At the very beginning of the snippet we called for two important global variable ($authordata & $post) to retrieve data. Through get_posts query and with the help of it’s parameters we gathered out desired posts and then simply ran a foreach loop to display them with their hyperlink. That’s all. Taking questions now.

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.