When it comes to WordPress, i have always discouraged people from using plugins unless they have no option. A good plugin can be hard to find as most plugins have really limited features for free plans but rather provide good features for paid plans which might not sound right to you.

How to display related posts in Wordpress

Poorly coded plugins can consume a lot of server resources as well and make you prone to phama-harks. Some take long to receive updates implicating newer versions of WordPress are likely to break it’s operation.

One good thing with writing your own code is it makes it easy for you to customize it and you are sure the code will always work unless WordPress makes changes on some of it’s codex which is more like impossible. In this article i explain how to get related posts by category and by tag.

1 – Get Related Posts by Tag

Please paste the code below where you want the related posts to appear in your theme’s single post template (single.php) file.

<?php
// My Related posts function

function my_related_posts() {
    global $post;
    $tags = wp_get_post_tags( $post->ID );
    if($tags) {
        foreach( $tags as $tag ) {
            $tag_arr .= $tag->slug . ',';
        }
        $args = array(
            'tag' => $tag_arr,
            'numberposts' => 3, /* You can change this to show more */
            'post__not_in' => array($post->ID)
        );
        $my_related_posts = get_posts( $args );
        if($my_related_posts) {
        echo '<h4>Related Posts</h4>';
        echo '<ul id="related-posts">';
            foreach ( $my_related_posts as $post ) : setup_postdata( $post ); ?>
                <li class="related_post">
                    <?php echo the_post_thumbnail(array(150,100)); ?>
                    <a class="entry-unrelated" href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                    <p><?php the_excerpt(); ?></p>                    
                </li>
            <?php endforeach; }
            }
    wp_reset_postdata();
    echo '</ul>';
}
?>

This code is similar to Eddie Machado’s “Bones” WordPress theme except that i added excerpt and post thumbnail functionality to it.

2 – Get Related Posts by Category

The code below will get related posts by a category and order the posts randomly.

<?php 

$orig_post = $post;
global $post;

$categories = get_the_category($post->ID);

if ($categories) {
   $category_ids = array();
       foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
       $args=array(
                    'category__in' => $category_ids,
                    'post__not_in' => array($post->ID),
                    'posts_per_page'=> 4, // Number of related posts that will be displayed.
                    'caller_get_posts'=>1,
                    'orderby'=>'rand' // Randomize the posts
       );

$my_query = new wp_query( $args );

   if( $my_query->have_posts() ) {
       echo '<div id="related-post">';
             while( $my_query->have_posts() ) {
                    $my_query->the_post(); ?>
                    <li>                      
                       <div class="related">
                            <a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
             </div>
                     </li>
<?php } ?>

<?php echo '</ul></div>';
} }
$post = $orig_post;
wp_reset_query(); ?>