Categories: Wordpress Development

Get Related Posts by TAGS in WordPress without Plugin

In my last tutorial i have described how to get related posts by category. In this post i will show you to get related posts by Tags without any plugins, keeping everything simple, light and accessible.

Lets say we want to add a related posts section at the end of each blog post we create. After each post we will insert 5 related posts that will be determined based on their content. To decided which articles are related, we will compare the articles’s “tags.”

Let’s get started!

 
$tags = wp_get_post_tags($post->ID);

if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

$args=array(
'tag__in'     => $tag_ids,
'post__not_in'    => array($post->ID),
'showposts'    =>5,
'ignore_sticky_posts' =>1
);

$my_query = new WP_Query($args);

if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" class="postlistlink" rel="bookmark"><?php the_title(); ?></a>
<?php
endwhile;
}
wp_reset_query();

}

So simple right. Still if you have any problem to understand, just drop a message below.
Thanks.

This post has already been read 2647 times!

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

More Posts

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

Share
Published by
Mehedi Hasan

Recent Posts

WP Share A Friend – A Way to Capture Lead

What is Lead Capture: In general sense lead capture designates a process or way of…

5 years ago

Unlimited Modal POPUPS on MouseClick

Introduction Unlimited Modal POPUPS on MouseClick: Everyone knows about popup during site visits and most…

5 years ago

WordPress Pregnancy Calculator Plugin

Brief about Pregnancy Calculator : This WordPress Pregnancy Calculator plugin allows users to display an…

5 years ago

Interactive Service Plugin with Hover Effects VC

Introduction About this Service Plugin: This Visual Composer Service Plugin works as an addon to…

6 years ago

WP Dynamic Query String

Introduction of Dynamic Query String : This plugin allows user to set dynamic query string…

6 years ago

How to make scroll PDF in iFrame for iPAD Safari

There is an occasional bug for iPhone and iPad even they are designed as well and…

7 years ago