Thursday, December 16, 2010

How to Show Related Posts in WordPress Without Using Plugin

related-posts-wordThere are many plugins that allow you to display related posts on your site, but most of them are either too bloated, too complicated or too simple. A better way is to implement the related posts function directly into your theme and reduce the server overhead. In this way, you can also theme it up the way you want it.

This tutorial will teach you how you can show related posts in your site without using any plugin.

First, open your functions.php file in your theme folder. Insert the following code:

[php]function get_related_post() //function to retrieve related post based on tags. Must use within loops.
{ global $wpdb,$post;
$posts_to_fetch = 5; //change this value to the number of related posts you want to fetch.
$tags = wp_get_post_tags($post->ID);
if ($tags)
{ $tag_list = '';
for($i=0;$i<count($tags);$i++)
{ $tag_list .= $tags[$i]->term_id;
if($i!=count($tags)-1) $tag_list .= ',';
}
$related_tags_query = "
SELECT $wpdb->posts.*, SUM(CASE WHEN $wpdb->term_relationships.term_taxonomy_id in (" . $tag_list . ") THEN 1 END) AS matchnum from $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.ID != " . $post->ID . "
GROUP BY $wpdb->term_relationships.object_id
HAVING SUM(CASE WHEN $wpdb->term_relationships.term_taxonomy_id in (" . $tag_list . ") THEN 1 END) > 0
ORDER BY matchnum DESC, $wpdb->posts.post_date DESC
LIMIT " . $posts_to_fetch . ";
";
$related_posts = $wpdb->get_results($related_tags_query);
return $related_posts;
}

function show_related_post($thumbnail = 0)
{ $got_related_posts = get_related_post();
if($got_related_posts)
{ echo '<ul class="related_posts">'
foreach($got_related_posts as $related_post)
{ echo '<li>';
if($thumbnail && has_post_thumbnail($related_post->ID))
{ the_post_thumbnail($related_post->ID);
echo '<br/>';
}
echo '<a href="'.get_permalink($related_post->ID).'">';
echo $related_post->post_title;
echo '</a>';
}
echo '</ul>';
}
}[/php]

The first function get_related_post query the database and fetch the posts that matched the current post's tags. The second function show_related_post is where you display the related posts on your site. Note that the second function must be used in the loop.

To display the related posts, open the single.php and place the tag <?php show_related_post(); ?> in the location where you want the related entries to show up. If you are using the featured image in each post, you can also display the thumbnail image with <?php show_related_post(true); ?>.

The displayed related posts list comes with a class of related_posts so you can style it up in your stylesheet.

Note: The above functions query the database everytime a post is loaded. It is best used with cache plugin (such as W3 Total Cache or Wp Super Cache) to reduce the server overhead.

Inspired by the Related Posts By Tags plugin.

Image credit: Maria Reyes-McDavis

No comments:

Post a Comment