Related Posts display easily with plugins but as a theme developer, you need to know, How to display related posts in WordPress without plugins. Many Plugins like jetpack provided related posts service. If you want to display related posts in WordPress as your own design. That Time you needed to use custom PHP code.
You can display related posts in many ways like WordPress Post Tags and categories. In this article, We will display related posts by tag and categories.
Posts Tags Related Post:
Posts are WordPress default post types. It has taxonomy-name tags by default. Each post can add one or multiple tags. Tag names must be unique. We will show related posts that have the same tags. This code finds other posts by wp_query.
Use this code where you want to see related posts.
<?php
global $post;
$tags = wp_get_post_tags($post->ID);
if(!empty($tags)):
$term_id = array();
foreach ($tags as $tag) {
$term_id[] = $tag->term_id;
}
$args = array(
'tag__and' => $term_id,
'post__not_in' => array($post->ID),
'posts_per_page' => 3,
'ignore_sticky_posts' => 1,
'post_status' => 'publish',
);
$custom_query = new wp_query( $args );
if($custom_query->have_posts()):
?>
<div>
<h3><?php echo esc_attr__('Related Posts', 'Text Domain'); ?></h3>
<ul style="display: flex; width: 100%; flex-wrap: wrap; list-style: none;">
<?php while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
<li style="flex: 0 0 33.333333%; max-width: 33.333333%; padding-right: 20px;">
<?php if(has_post_thumbnail()): ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title() ?>">
<?php the_post_thumbnail('post-thumbnail'); ?>
</a>
<?php endif; ?>
<h4><a href="<?php the_permalink(); ?>" title="<?php the_title() ?>"><?php the_title(); ?></a></h4>
<p>Posted by: <?php echo get_the_author_meta('display_name'); ?> Posted on: <?php the_date(); ?></p>
<p><?php the_excerpt(); ?></p>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php
endif;
endif;
wp_reset_query();
?>
Category Related Posts:
Every post has a category taxonomy. Webmasters can select categories. This algorithm can search posts in the same category.
<?php
global $post;
$categories = get_the_category($post->ID);
if(!empty($categories)):
$cat_ID = array();
foreach ($categories as $cat) {
$cat_ID[] = $cat->term_id;
}
$args = array(
'category__in' => $cat_ID,
'post__not_in' => array($post->ID),
'posts_per_page' => 3,
'ignore_sticky_posts' => 1,
'post_status' => 'publish',
);
$custom_query = new wp_query( $args );
if($custom_query->have_posts()):
?>
<div>
<h3><?php echo esc_attr__('Related Posts', 'Text Domain'); ?></h3>
<ul style="display: flex; width: 100%; flex-wrap: wrap; list-style: none;">
<?php while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
<li style="flex: 0 0 33.333333%; max-width: 33.333333%; padding-right: 20px;">
<?php if(has_post_thumbnail()): ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title() ?>">
<?php the_post_thumbnail('post-thumbnail'); ?>
</a>
<?php endif; ?>
<h4><a href="<?php the_permalink(); ?>" title="<?php the_title() ?>"><?php the_title(); ?></a></h4>
<p>Posted by: <?php echo get_the_author_meta('display_name'); ?> Posted on: <?php the_date(); ?></p>
<p><?php the_excerpt(); ?></p>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php
endif;
endif;
wp_reset_query();
?>
If you don’t understand, what is the difference between these two algorithms? Let’s explain.
How to work related Posts:
Every post has a Post ID. Using this Post ID we will find all post tags or post categories. Those two function names get_the_category and wp_get_post_tags . Then we all tag ID or category ID list with an array.
To get all related posts we use wp_query class or you can use the get_posts function. That can automatically search all posts.
You can use these algorithms as a template. And include them on single.php Using get_template_part function.
FAQs:
Related posts by taxonomy. It’s mean a post with other post relations by taxonomy. It’s loads all related posts with the same taxonomy.
Conclusion:
If you create a new project or work with clients. This is really helpful. You can use and modify these algorithms. If you have any questions about it we will ready to help you.
We hope this article will help you. Please comment below.
Thanks for your very informative guidelines
Thanks