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!
What is Lead Capture: In general sense lead capture designates a process or way of…
Introduction Unlimited Modal POPUPS on MouseClick: Everyone knows about popup during site visits and most…
Brief about Pregnancy Calculator : This WordPress Pregnancy Calculator plugin allows users to display an…
Introduction About this Service Plugin: This Visual Composer Service Plugin works as an addon to…
Introduction of Dynamic Query String : This plugin allows user to set dynamic query string…
There is an occasional bug for iPhone and iPad even they are designed as well and…