Home » Cybersecurity » How To Display Related WordPress Posts Without Plugins
You recommend a WordPress plugin for that

How To Display Related WordPress Posts Without Plugins

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
 );

Jared Harrison

Jared Harrison is an accomplished tech author and entrepreneur, bringing forth over 20 years of extensive expertise in cybersecurity, privacy, malware, Google Analytics, online marketing, and various other tech domains. He has made significant contributions to the industry and has been featured in multiple esteemed publications. Jared is widely recognized for his keen intellect and innovative insights, earning him a reputation as a respected figure in the tech community.

More Reading

Post navigation

3 Comments

  • 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.

  • Hello, Sean Davis,
    This is very helpful for me…but here you have added the Category code in tags section and Tag code in Category section. Please change it. Thanks a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *

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

The best bbPress shortcodes for WordPress

How To Login To WordPress With An Email Address (Enabling Email Addresses)

How To Display BBPress Forum Topics Using PHP On WordPress