How to show related post in WordPress

Posted in WordPress · January 8th, 2011 by Kazim · Comments (6)

So as I was looking after a simple code to show related post in WordPress. I didn’t find any good little good to do it, where as guys around were providing a huge code to do. I’ve just coded a little piece of code. Hope this will be helpful for you. Let’s start.

It’s so easy to understand. As per WordPress Get Posts function. You can use it for following purposes.

  • Show posts from a category
  • Describe number of posts to show
  • Show posts by status i.e show draft posts, show pending posts, show schedule posts, show publish posts.
  • Display posts by a meta key
  • Display posts by meta value
  • Exclude posts to show
  • Include posts to show
  • Show children of post by specific post ID.
  • Hide or show paging
  • Show posts by attachment
  • Show posts by category name
  • Show posts by TAGS
  • Order posts by author, category, content, date, post ID, menu order, attachments, date of modify, name, parent, password, random order, status, title & by type. You can have ascending and descending order.

No, this don’t just seem to be useful. This function is really useful for coders.  Obviously if a post has same tags means they’re related. For example you’re typing your post for android will contain the tag ‘android’. So when you will create another post on android it will show you the post contains the tags of your current post i.e you’re writing about android and your post tags are android, touch & Wi-Fi. So this will show you all of posts contain tags android, touch & Wifi.

I’m making it easy for you. You will be able to show related post in WordPress by using 3 simple steps below.

<h3> Related Posts</h3>
<ul>
<?php
$tags = wp_get_post_tags($post->ID);
global $post;
$args = array( 'posts_per_page' => 5, 'tags'=> $tags, 'exclude'=> $post->ID );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :	setup_postdata($post); ?>
	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
</ul>
  • Copy above code
  • Go to Theme Editor of your WordPress I.E Appearance > Editor
  • Paste the code where ever you want. I recommend you to paste this code after below line
 <?php the_tags(

This will benefit your the clicks, More over you can place your 250×250 Google Ads on left side and related post on right side. This will help you increasing your Adsense earning and will also bring up traffic from one to another post.

As per the comment of our good reader, Jitendra Kumar from Shoutingwords saying “The code is good but it’s showing the currently opened post in related post too. Hence need to exclude”. Thanks to Jitendra. After reviewing some more to the code, as I got notified that comment function hasn’t been working fine. The code has now been amended and perfect!

Tags: , , , , , , , , , , , , , , , , , , ,

Related Posts



6 Responses to “How to show related post in WordPress”

  1. Jitendra Kumar Says:

    Hi,
    First of all thanks for sharing this code. This code is good but it has a problem, it also lists the current page which should not be there since the “current post could not be related to the current post”. To correct this, use the following where you are declaring the array:

    $args = array( ‘posts_per_page’ => 5, ‘tags’=> $tags, ‘post__not_in’ => array($post->ID) );

    This will correct your code and will not show current post.

  2. Jitendra Kumar Says:

    Thanks Kazim, the code is perfect now. :-)

  3. Raymund Says:

    Hi,

    Just want to ask how to put adsense and related posts side by side. Just like what you do here in your blog.

  4. Kazim Says:

    Dear Raymund,

    Regarding the adsense and related post side by side is through float function of css. Likewise you can do float:left to ads and float:right to adsense and giving both divs some width would work! For an example, you can check the source code of this page. You will know it.

    Thanks
    Kazim

  5. Paul Says:

    Do you prefer adding related posts with tags or by category?

  6. Kazim Says:

    Paul, depends on what you want. Few people prefer to have the related post with tags, cause if you have the tag ‘WordPress’ then other post with the tag wordpress will be displayed. Same goes for the category, If your post is in WordPress category, showing relating post by category would be awesome.

    How about an idea of Tag & Category mixup?

Leave a Reply