How to Create a Wordpress Related Post Plugin Like A Boss?

This guest post is written by Malinda Alahakoon of

Previously I gave you a detailed step by step guide to prepare your environment to customize Wordpress (self-hosted) like a BOSS. Here we will be customizing the theme you have installed to give it unique features using PHP, HTML and CSS coding.

Wordpress Related Post Plugin

You might ask me “Why should I deal with geeky coding when I can simply install a plug-in?” Yeah, you have a valid point. But once you create your own plug-in you are the God and you make the rules. You can do whatever you wish for. Any sort of customization is just few lines of code away. Most importantly you can give wings to your imagination and develop unique features.

Today you will learn how to customize Wordpress theme step by step and add a new functionality. If you didn’t read “Getting Started with Advanced Wordpress Customization”, it’s time to go back and take a look. If you got some time to play with PHP and CSS, it would be a great support.

I’ll start developing related post plug-in. It’s very important to have one to reduce your bounce rate and deliver a great user experience.

Tip: 4 Ultimate Techniques to Get Traffic With Twitter

Steps to Develop Related Post Plugin

First open Dreamweaver and connect to remote site. There you have to open the file wp-content/theme/yourtheme/function.php. This could be custom_functions.php in some themes. There you will be able to see lots of theme specific operations.
Here I’m going to tell you how to create a related post plug-in. I’m going to find related posts using matching tags.

 Develop Related Post Plugin

  • Scroll down to page and create a new PHP function. Give it an appropriate name.
  • Here is the PHP code to retrieve related posts based on tags. Read the comments to figure out what’s happening in each line.
Tip: 10 Features of a Unique and Strong Brand

function related_posts(){
global $post;
$current_post = new wp_query('p='.$post->ID); // retrieve current post
$tags = wp_get_post_tags($post->ID); // retrieve tags in current post
if ($tags) { // check whether current post have any tags. If not, no point proceeding further
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; // put tags in current posts to php array
$args=array( // argument list to retreive related posts
'tag__in' => $tag_ids, // post with this tags
'post__not_in' => array($post->ID), // omit current post
'showposts'=>5, // Number of related posts that will be shown.
$my_query = new wp_query($args); // query db to get related posts and store them in my_query
if( $my_query->have_posts() ) { // check whether there are any related posts
echo '<h2>Read Related Posts</h2><div class="related_posts"><ul>'; // title
while ($my_query->have_posts()) { // display related posts one by one
<div class="related_post_item">
<a href="<?php the_permalink() ?>" rel=bookmark title="<?php the_title_attribute(); ?>"><li><?php the_title(); ?></li></a>
echo '</ul></div>';
$current_post->the_post(); // Reset current post or your comment thread and other functions comes after this will get messed up

  • Now your function is ready to display related posts as a simple unordered list.
  • Next part is to include this in your posts page. For that open single.php.
  • Include below code wherever you want to display related posts.

<?php related_posts();?>

  • Now upload all the files to server and you are DONE!
  • Go to any posts page and you will be able to see related posts items as a list.

Now you might start thinking how to take this to next level. For that you will need some CSS knowledge and it will be highly dependent on other theme functions. In my blog I have created below related posts items by adding post thumbnail.

Tip: Top 10 Companies That People Die To Work In!

wordpress related post widget

Unfortunately I have no way to give you any more tips since they will be based on your theme. If you have a creative idea and you need help to implement it, never hesitate to contact me @MalindaOnline. I’ll assist you to create what you exactly need by studying your surrounding theme elements.

You may like to read:

Malinda Alahakoon

Malinda Alahakoon is a regular BloggingeHow reader and follower who loves to write on his blog NotesOfGenius. His passion for Blogging is what makes him stand out. You can connect with him @MalindaOnline



AdSense affiliate marketing Announcement AskHassam Blogger Blogging Tips Blogosphere News Bluehost Building Traffic BuySellAds Contest Customizations Disqus Tutorials Domain Hosting Drop Down Menus eBook Email Marketing Facebook Tutorials Feedburner Freelancing Giveaway Google Google Analytics Google Gmail Google Plus Tricks Guest Post HowTo Interview Making Money Online Monetization Motivation PayPal Pinterest Plugins review SEO Slideshare Social Media Top Lists Twitter Wordpress YouTube
BloggingeHow | Making Blogging Simpler: How to Create a Wordpress Related Post Plugin Like A Boss?
How to Create a Wordpress Related Post Plugin Like A Boss?
BloggingeHow | Making Blogging Simpler
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy

Tools of the Trade