Categories: Wordpress Development

Get Related Post by Category in WordPress without Plugin

Interlinks in your blog articles will allow all types visitors to get related content to the current post. This will help to build up page views on your blog and build most visitor engagement.

But it is time consuming to engage visitors if you do not have proper links in blog to pick the related post. So you have to make sure you link correctly to related posts. And WordPress have algorithm that would find other posts within the same category as the current post, and it will list them as related posts.

Every post that you put up on your blog should have a one or multi category. Therefore if you can match these category to other blog posts you will easily be able to find posts which are similar to the current post.

The below code uses post’s category as a criteria to fetch out the similar posts. This is really useful if each of your post belongs to at-least one category. Using this, you can ensure that all the displayed related posts would belong to the same category, this would improve the on page seo too.

<?php

$catArgs = array(
 'category__in' => wp_get_post_categories($post->ID),
 'showposts' => 5,//display number of posts
 'orderby' =>'rand',//display random posts
 'post__not_in' => array($post->ID)
 ); 

$cat_post_query = new WP_Query($catArgs); 

if( $cat_post_query->have_posts() ) { 
 while ($cat_post_query->have_posts()) : $cat_post_query->the_post(); ?> 
  <li> <a href="<?php the_permalink() ?>"> <?php the_title(); ?></a></li> 
<?php endwhile; 

wp_reset_query(); } ?>

Still if you have any problem to set up, please drop a message bellow. Hope we can solve your any problem.

Thanks.

This post has already been read 5075 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
Tags: categorypost

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