{"id":338,"date":"2009-04-17T23:30:29","date_gmt":"2009-04-17T18:00:29","guid":{"rendered":"http:\/\/www.bin-co.com\/blog\/?p=256"},"modified":"2009-04-17T23:30:29","modified_gmt":"2009-04-17T18:00:29","slug":"show-related-post-in-wordpress-without-a-plugin","status":"publish","type":"post","link":"https:\/\/www.bin-co.com\/blog\/2009\/04\/show-related-post-in-wordpress-without-a-plugin\/","title":{"rendered":"Show Related Post in WordPress Without a Plugin"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.bin-co.com\/blog\/wp-content\/uploads\/2009\/03\/wordpress-150x150.jpg\" alt=\"WordPress Metal Logo\" title=\"WordPress Metal Logo\" width=\"150\" height=\"150\" class=\"alignnone size-full wp-image-239 intro\" align=\"right\" \/><\/p>\n<p class=\"intro\">Related posts is a very popular feature. My default wordpress installation often includes a plugin that has this functionality. There are quite a few plugins that lets you have this feature&#8230;<\/p>\n<ul>\n<li><a href=\"http:\/\/mitcho.com\/code\/yarpp\/\">Yet Another Related Posts Plugin (YARPP)<\/a><\/li>\n<li><a href=\"http:\/\/rmarsh.com\/plugins\/similar-posts\/\">Similar Posts<\/a><\/li>\n<li><a href=\"http:\/\/blendworx.com\/aizattos-related-posts-wordpress-plugin\/\">Aizattos Related Posts<\/a><\/li>\n<li><a href=\"http:\/\/wordpress.org\/extend\/plugins\/wordpress-23-related-posts-plugin\/\">WordPress Related Posts<\/a><\/li>\n<li><a href=\"http:\/\/wasabi.pbwiki.com\/Related%20Entries\">Wasabi Related entries<\/a><\/li>\n<\/ul>\n<h2>The Code<\/h2>\n<p>WordPress has been supporting tags in its new released &#8211; so the related posts feature can be implemented without the help of any plugins. All we have to do is find the <strong>other posts with some same tags as the current post<\/strong>. Just open the <strong><code>single.php<\/code> file in your theme and add this bit of code<\/strong> where you want the related posts to show up&#8230;<\/p>\n<pre><code class=\"php\">&lt;?php\n$tags = wp_get_post_tags($post-&gt;ID);\nif ($tags) {\n\t$tag_ids = array();\n\tforeach($tags as $individual_tag) $tag_ids[] = $individual_tag-&gt;term_id;\n\t\n\t$args=array(\n\t\t'tag__in' =&gt; $tag_ids,\n\t\t'post__not_in' =&gt; array($post-&gt;ID),\n\t\t'showposts'=&gt;5, \/\/ Number of related posts that will be shown.\n\t\t'caller_get_posts'=&gt;1\n\t);\n\t$my_query = new wp_query($args);\n\tif( $my_query-&gt;have_posts() ) {\n\t\techo '&lt;h3&gt;Related Posts&lt;\/h3&gt;&lt;ul&gt;';\n\t\twhile ($my_query-&gt;have_posts()) {\n\t\t\t$my_query-&gt;the_post();\n\t\t?&gt;\n\t\t\t&lt;li&gt;&lt;a href=\"&lt;?php the_permalink() ?&gt;\" rel=\"bookmark\" title=\"Permanent Link to &lt;?php the_title_attribute(); ?&gt;\"&gt;&lt;?php the_title(); ?&gt;&lt;\/a&gt;&lt;\/li&gt;\n\t\t&lt;?php\n\t\t}\n\t\techo '&lt;\/ul&gt;';\n\t}\n}\n?&gt;<\/code><\/pre>\n<p>This code finds the other post with any one of the tag that the current post has. If you want to show the <strong>posts with any one of the categories<\/strong> that the current post has, use this code instead&#8230;<\/p>\n<pre><code class=\"php\">&lt;?php\n$categories = get_the_category($post-&gt;ID);\nif ($categories) {\n\t$category_ids = array();\n\tforeach($categories as $individual_category) $category_ids[] = $individual_category-&gt;term_id;\n\t\n\t$args=array(\n\t\t'category__in' =&gt; $category_ids,\n\t\t'post__not_in' =&gt; array($post-&gt;ID),\n\t\t'showposts'=&gt;5, \/\/ Number of related posts that will be shown.\n\t\t'caller_get_posts'=&gt;1\n\t);\n\t\/\/ Rest is the same as the previous code\n<\/code><\/pre>\n<h2>WordPress Plugin Killer Series<\/h2>\n<p>For those who came in late, this post is part of the <a href=\"http:\/\/www.bin-co.com\/blog\/2009\/03\/wordpress-plugin-killer-series\/\">WordPress Plugin Killer Series<\/a>. This series will show you how to duplicate the functionality of the a few wordpress plugins without having to install it using custom code in the wordpress theme. The previous posts in this series are&#8230;<\/p>\n<ul>\n<li><a href=\"http:\/\/www.bin-co.com\/blog\/2009\/02\/avoid-duplicate-content-use-canonical-url-in-wordpress-fix-plugin\/\">Avoid Duplicate Content &#8211; Use Canonical URL in WordPress<\/a><\/li>\n<li><a href=\"http:\/\/www.bin-co.com\/blog\/2009\/03\/show-popular-posts-in-wordpress-without-a-plugin\/\">Show Popular Posts in WordPress &#8211; without a plugin<\/a><\/li>\n<li><a href=\"http:\/\/www.bin-co.com\/blog\/2009\/03\/adding-social-bookmarking-button-in-wordpress-without-plugins\/\">Adding Social Bookmarking Button in WordPress &#8211; Without Plugins<\/a><\/li>\n<\/ul>\n<h3>More Resources<\/h3>\n<ul>\n<li><a href=\"http:\/\/www.smashingmagazine.com\/2009\/04\/15\/10-exceptional-wordpress-hacks\/\">10 Exceptional WordPress Hacks<\/a><\/li>\n<li><a href=\"http:\/\/www.wprecipes.com\/how-to-show-related-posts-without-a-plugin\">How to: Show related posts without a plugin<\/a><\/li>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Related posts is a very popular feature. My default wordpress installation often includes a plugin that has this functionality. There are quite a few plugins that lets you have this feature.<\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,25,31],"tags":[212,213,219,235,251,304],"class_list":["post-338","post","type-post","status-publish","format-standard","hentry","category-php","category-scripts","category-wordpress","tag-plugin","tag-plugin-killer","tag-post","tag-related","tag-series","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/comments?post=338"}],"version-history":[{"count":0,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/338\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/media?parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/categories?post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/tags?post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}