Would you like your website to feel so engaging that visitors don’t want to leave— you should How to Show Related Posts in WordPress, just like sitting in a cozy café with a good book? Here’s a simple yet powerful technique that can help you create that kind of experience: displaying related posts in WordPress.
By showing users content that closely matches their interests, you not only improve the user experience but also increase page views, reduce bounce rate, and even enhance SEO performance.
In this tutorial, you’ll learn how to display related posts in WordPress without using any plugins—just a bit of clean and efficient code.

Table Of Contents
Step 1: Enable Featured Images in Your Theme
The first step to Show Related Posts in WordPress is to make sure your theme supports featured images (also known as post thumbnails). To do this, open your theme’s functions.php file and add the following lines of code:
This code enables featured images and sets their default size, allowing your theme to display attractive thumbnails for each post. (the Most of the Modern themes have these by default and you don’t need to add it, if your posts has featured images you can skip this step )
You Can Use Plugins Like WP Code to add this code without customizing your theme files.
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 260, 180, true );
Step 2: Add Related Posts Code to single.php
Next, we’ll Show Related Posts in WordPress based on shared tags with the current post.
Editing Theme Files
Open the single.php file in your theme (its located in wp-content/themes/your-theme-name/single.php ) and locate the following line:

<?php endwhile; ?>
Just before this line, insert the following code snippet:
<div class="relatedposts">
<h3>Related Posts</h3>
<?php
$orig_post = $post;
global $post;
$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),
'posts_per_page' => 4,
'caller_get_posts' => 1
);
$my_query = new wp_query($args);
while($my_query->have_posts()) {
$my_query->the_post();
?>
<div class="relatedthumb">
<a rel="external" href="<?php the_permalink() ?>">
<?php the_post_thumbnail(array(260, 180)); ?><br />
<?php the_title(); ?>
</a>
</div>
<?php
}
}
$post = $orig_post;
wp_reset_query();
?>
</div>
Without Touching Theme Files ( Cleaner way )
There is a way you can add the code to single.php without editing theme files, it will save the code for next updates of your theme too.

You can add php code using WP Code Plugin and use wordpress actions. go to plugin snippets section and add a php snippet. then add this code :
function koolak_add_related_posts_to_content($content) {
if (is_single() && is_main_query()) {
global $post;
$orig_post = $post;
$tags = wp_get_post_tags($post->ID);
$related_html = '<div class="relatedposts"><h3>Related Posts</h3>';
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),
'posts_per_page' => 4,
'ignore_sticky_posts' => 1
);
$my_query = new WP_Query($args);
while ($my_query->have_posts()) {
$my_query->the_post();
$related_html .= '<div class="relatedthumb">';
$related_html .= '<a rel="external" href="' . get_permalink() . '">';
if (has_post_thumbnail()) {
$related_html .= get_the_post_thumbnail(get_the_ID(), array(260, 180)) . '<br />';
}
$related_html .= get_the_title() . '</a></div>';
}
wp_reset_postdata();
}
$related_html .= '</div>';
// Restore original post
$post = $orig_post;
return $content . $related_html;
}
return $content;
}
add_filter('the_content', 'koolak_add_related_posts_to_content');
This code dynamically fetches and displays up to 4 related posts that share tags with the current post. Each result includes a clickable thumbnail and title.
Step 3: Style the Related Posts Section
Now that your related posts are working and Show Related Posts in WordPress is Done, let’s make them look polished. Add the following styles to your theme’s style.css file or you can use plugins like WP Code to add codes without touching theme files. it will save these for next updates too.
.relatedposts {
width: 100%;
overflow: hidden;
margin: 20px 0;
padding: 20px;
background-color: #f9f9f9;
}
.relatedposts h3 {
margin-bottom: 15px;
color: #333;
}
.relatedthumb {
width: 23%;
float: left;
margin: 0 1%;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.1);
}
.relatedthumb img {
width: 100%;
height: auto;
}
.relatedthumb a {
text-decoration: none;
color: #000;
display: block;
padding: 5px;
}
.relatedthumb a:hover {
background-color: #eee;
}
These styles ensure that your related posts section is clean, organized, and visually appealing across all screen sizes. i used very simple CSS codes, you can tweak it using your CSS knowledge.
Why This Helps Your SEO
Showing related posts doesn’t just improve user experience—it also helps with SEO. When users stay longer on your site and visit more pages, search engines view your content as more valuable and engaging. This can lead to higher rankings in search results.
In short, by keeping visitors engaged and guiding them through your content, you’re creating a content ecosystem that works both for your users and for search engines.
Final Thoughts on Show Related Posts in WordPress
With just a few lines of code and a little styling, you can transform your WordPress blog into an engaging, user-friendly space that encourages visitors to stay longer and explore more.
Here’s what you’ve accomplished:
- Added support for featured images
- Displayed related posts without plugins
- Styled your related content section for a better visual experience
- Improved your site’s SEO
Always remember to prioritize user experience. A well-designed, easy-to-navigate website keeps users coming back—and search engines happy. We Have A Lot of articles on “WordPress Performance Optimization” Topic in our website so you can gain more users and improve you SEO
And finally, stay up to date with the latest WordPress and SEO trends. Your website is a living space that thrives with regular attention and care.
Good luck, and here’s to a more engaging WordPress website!
FAQs About How to Show Related Posts in WordPress Without a Plugin (Step-by-Step Guide)
Q: Can I show related posts based on categories instead of tags?
A: Yes. You can modify the code to use categories instead of tags by replacing the tag__in argument with category__in, and retrieving category IDs using wp_get_post_categories()
Q: Will this method slow down my website?
A: No, this method uses a lightweight custom query without relying on third-party plugins, making it more efficient and faster than plugin-based solutions.
Q: Where should I place the code for best results?
A: The code should be placed inside your theme’s single.php file, just before , to ensure it appears after the main content of the post.
Q: What happens if a post has no tags?
A: If a post doesn’t have any tags, the related posts section will not appear, since the query requires shared tags to fetch related content.
Q: Can I display more or fewer related posts?
A: Yes, simply change the posts_per_page value in the query to control how many related posts are displayed (e.g., posts_per_page => 6 for six posts).