You recommend a WordPress plugin for that

How To Display Related WordPress Posts Without Plugins

There are many ways to display recent posts on WordPress without using unnecessary recent posts plugins that are notorious for slowing down WordPress websites. In this article we will discuss how to display related posts on a list per category and tag using simple PHP that can be implemented into theme files such as single.php, category.php, and others.

There are many ways to display related posts on WordPress without using unnecessary related posts plugins that are notorious for slowing down WordPress websites. In this article we will discuss how to display related posts on a list per category and tag using simple PHP that can be implemented into theme files such as single.php, category.php, and others.

Display Related Posts In Same Category

To show related posts on WordPress by category, copy and paste the code below into a PHP template file.

<?php
$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, // Number of related posts that will be shown.
 'caller_get_posts'=>1
 );
 $my_query = new wp_query($args);
 if( $my_query->have_posts() ) {
 echo '<h3>Related Posts</h3><ul>';
 while ($my_query->have_posts()) {
 $my_query->the_post();
 ?>
 <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
 <?php
 }
 echo '</ul>';
 }
}
?>

To add related posts to published WordPress posts automatically paste this code to your preferred location on the single.php file.

Display Related Posts Using The Same Tags

To display related posts per associated tag used, copy and paste the code below into a template file.

<?php
$categories = get_the_category($post->ID);
if ($categories) {
 $category_ids = array();
 foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;

 $args=array(
 'category__in' => $category_ids,
 'post__not_in' => array($post->ID),
 'showposts'=>5, // Number of related posts that will be shown.
 'caller_get_posts'=>1
 ); 
 $my_query = new wp_query($args);
 if( $my_query->have_posts() ) {
 echo '<h3>Related Posts</h3><ul>';
 while ($my_query->have_posts()) {
 $my_query->the_post();
 ?>
 <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
 <?php
 }
 echo '</ul>';
 }
}
?>

Changing The Amount Of Related Posts Displayed

To change the amount of posts displayed switch the 5 on line 10 to your preferred number.

 'showposts'=>5, // Number of related posts that will be shown.

Adding Some Randomness

To make related posts refresh in a more random manner insert the extra code below directly above caller_get_posts.

'orderby'=> rand,

Here’s an example:

$args=array(
 'tag__in' => $tag_ids,
 'post__not_in' => array($post->ID),
 'showposts'=>3, // Number of related posts that will be shown.
 'orderby'=> rand,
 'caller_get_posts'=>1
 );

Sean Doyle

Sean is a tech author and security researcher with more than 20 years of experience in cybersecurity, privacy, malware analysis, analytics, and online marketing. He focuses on clear reporting, deep technical investigation, and practical guidance that helps readers stay safe in a fast-moving digital landscape. His work continues to appear in respected publications, including articles written for Private Internet Access. Through Botcrawl and his ongoing cybersecurity coverage, Sean provides trusted insights on data breaches, malware threats, and online safety for individuals and businesses worldwide.

More Reading

Post navigation

  • Algieri, qui a battu Ruslan frapper Provodnikov pour la WBO Welterweight Champion Lumière en Juin, est également un ancien Karate Association Internationale du Sport champion (ISKA) mondiale Welterweight et World Kickboxing Association ( WKA) champion du monde des super welters, qui a encha?né dans la boxe et met au défi l’un des meilleurs boxeurs au monde, malgré seulement 20 matchs de boxe sous son ancien rival belt.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.