Post Treatment – Show Related Posts Without a Plugin

Using too many plugins on a WordPress installation usually slower the loading time of the site, especially when same can be achieved using a simple piece of code. Though its not always wise to use codes in your wordpress themes, especially if you are not familiar with them. Here we will show you how to Show Related Posts Without Plugin which will be based on the tags you will use in your posts. Hope this helps some.

Procedure:

Note: Do take backup of your theme before you try this.

01. Go to Appearance => Editor.
editor

02. Then choose functions.php file from right hand side and add below code:
functions.php

function related_posts_shortcode( $atts ) {
extract(shortcode_atts(array(
‘limit’ => ’5′,
), $atts));

global $wpdb, $post, $table_prefix;

if ($post->ID) {
$retval = ‘<ul>’;
// Get tags
$tags = wp_get_post_tags($post->ID);
$tagsarray = array();
foreach ($tags as $tag) {
$tagsarray[] = $tag->term_id;
}
$tagslist = implode(‘,’, $tagsarray);

// Do the query
$q = "SELECT p.*, count(tr.object_id) as count
FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p WHERE tt.taxonomy =’post_tag’ AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID
AND p.post_status = ‘publish’
AND p.post_date_gmt < NOW()
GROUP BY tr.object_id
ORDER BY count DESC, p.post_date_gmt DESC
LIMIT $limit;";

$related = $wpdb->get_results($q);
if ( $related ) {
foreach($related as $r) {
$retval .= ‘
<li><a title="’.wptexturize($r->post_title).’" href="’.get_permalink($r->ID).’">’.wptexturize($r->post_title).’</a></li>
‘;
}
} else {
$retval .= ‘
<li>No related posts found</li>
‘;
}
$retval .= ‘</ul>
‘;
return $retval;
}
return;
}
add_shortcode(‘related_posts’, ‘related_posts_shortcode’);


03. Save file. Now open single.php file and place following lines where you want your Related Posts to appear:
functions.php

[related_posts]

04. Save file and you are done.


For New WordPress Users:

You can try out following recommended plugins:

Yet Another Related Posts Plugin
Or
Similar Posts

Further Resources:

That concludes this simple and useful tutorial. Hope you like it.

  • Pingback: Tweets that mention new post) Post Treatment – Show Related Posts Without a Plugin #wordpress -- Topsy.com

  • Pingback: [User Link:Show Related Posts Without a Plugin] | Tips for Designers and Developers | tripwire magazine

  • Pingback: wp-popular.com » Blog Archive » Post Treatment – Show Related Posts Without a Plugin | TutZone

  • Pingback: CSS Brigit | Show Related Posts Without a Plugin

  • Pingback: 160+ Fresh Addictive Articles for Web Designers | tripwire magazine

  • http://wpblast.com wpBlast

    Very helpful post! I love finding ways to work around plugins.

  • http://wpblast.com wpBlast

    Very helpful post! I love finding ways to work around plugins.

  • http://www.tutzone.org/ Bariski

    Glad you like this.

  • http://www.tutzone.org/ Bariski

    Glad you like this.

  • http://www.goospoos.com Divyang

    That was useful. I'll try to use this on my blog.

  • http://www.goospoos.com Divyang

    That was useful. I'll try to use this on my blog.

  • johnsonkoh

    This is cool stuff buddy. I always try to reduce the no. of plugins used for my site.

  • johnsonkoh

    This is cool stuff buddy. I always try to reduce the no. of plugins used for my site.

  • http://www.w3mag.com W3Mag

    That's great! Minus a plugin in the endless plugins list! Thanks!

  • http://www.w3mag.com W3Mag

    That's great! Minus a plugin in the endless plugins list! Thanks!

  • http://www.gutscheintrends.de/haburi-gutschein/ Haburi

    Hey,

    this is great. It is more flexible and more related then the plugins I have tried before.
    Thanks for this nice snippet :)

    Cheers Haburi

  • http://www.gutscheintrends.de/haburi-gutschein/ Haburi

    Hey,

    this is great. It is more flexible and more related then the plugins I have tried before.
    Thanks for this nice snippet :)

    Cheers Haburi

  • http://www.tutzone.org/ Bariski

    Glad this helped you.

  • http://www.tutzone.org/ Bariski

    Glad this helped you.

  • http://www.tutzone.org/ Bariski

    Glad this helped you.

  • http://www.tutzone.org/ Bariski

    Glad this helped you.

  • http://www.tutzone.org/ Bariski

    You are welcome.

  • http://www.tutzone.org/ Bariski

    You are welcome.

  • http://www.tutzone.org/ Bariski

    Glad this helped you.

  • http://www.tutzone.org/ Bariski

    Glad this helped you.

  • http://www.gutscheinrausch.de/guess/ Guess

    Thanks for showing us how to show related posts without a plugin. Its really helpful, because when you modify plugins, you can no longer update it.

  • http://www.fancyecig.com electronic cigarette

    Thanks for sharing!!

  • Trisna

    Nice tutorial, but if you don't mind, would you please give a tutorial how to make related posts with thumbnails just like the related posts in this site. Thanks

  • http://aysseline.com Aysseline

    This site use YARPP plugin (see the link at end of the tuto). You can also take a look at this: http://wordpress.shaldybina.com/plugins/related

  • http://thecopypasteblog.com Rahul Banker

    just tried doing this and oops :( messed up my functions.php and now trying to get it back. hope all will be good! :) Thanks for the tutorial btw

  • Satish11nov

    Hi i have used the code but i am getting following error :

    Parse error: syntax error, unexpected ‘>’
    i have used function in function.php inside my theme folder .
    please help me