How to add related posts in WordPress

Last updated:

Home » Articles » Wordpress » How to add related posts in WordPress

Related posts sections help keep visitors engaged with your content by showing them similar articles they might be interested in. You can set it up by using one of the plugins below. These plugins are reliable and trusted by WordPress users.

YARPP is a popular related posts plugin with a robust algorithm:

  1. Go to Settings > YARPP
  2. Configure your settings across the three sections:
    • The Algorithm: Set match threshold (1-5, higher = more closely related), and choose which content elements to consider (categories and tags are recommended)
    • The Pool: Select which post types to include and any categories/tags to exclude
    • Automatic Display Options: Set maximum number of posts (3-4 recommended), choose display theme (List or Thumbnails), and customise HTML if needed

Note: While YARPP has a strong algorithm, it can be resource-intensive on larger sites. Consider using one of the other methods if your site has thousands of posts.

Another trusted and lightweight plugin for related posts is Related Posts for WordPress. It’s easy to set up and doesn’t impact performance much, even on larger sites.

  • Install and activate the plugin.
  • It automatically generates related posts based on categories and tags.
  • You can configure the display settings directly from the plugin settings page, including the number of related posts to show and the display style (List, Grid, or Thumbnails).

Using Jetpack Plugin

Jetpack is a comprehensive plugin that offers many features, including related posts. Here’s how to enable it:

  • Install and activate Jetpack on your WordPress site.
  • Go to Jetpack > Settings and find the Related Posts section.
  • Enable the feature and customise how many related posts you want to display.

Using code snippets plugin (even if you are not a developer)

If you want a code-based solution without editing theme files directly:

  1. Install and activate the “WP Code” plugin
  2. Go to +Add snippets -> Add your code snippets
  3. Name a title for the code the add the code to “Code preview” block
  4. Go the Insertion -> Page specific -> Insert after post
  5. Paste this optimised code:
function display_related_posts() {
    // Only run on single posts
    if (!is_single()) return;
    
    global $post;
    $current_post_id = $post->ID;
    $categories = get_the_category();
    
    if (!$categories) return;
    
    $category_ids = wp_list_pluck($categories, 'term_id');
    
    $args = array(
        'category__in'      => $category_ids,
        'post__not_in'      => array($current_post_id),
        'posts_per_page'    => 3,
        'orderby'           => 'date', // More efficient than 'rand'
        'order'             => 'DESC',
        'no_found_rows'     => true, // Performance improvement
        'update_post_meta_cache' => false, // Performance improvement
        'update_post_term_cache' => false, // Performance improvement
    );
    
    $related_query = new WP_Query($args);
    
    if ($related_query->have_posts()) {
        echo '<div class="related-posts"><h3>Related Posts</h3><ul>';
        while ($related_query->have_posts()) {
            $related_query->the_post();
            echo '<li><a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a></li>';
        }
        echo '</ul></div>';
    }
    wp_reset_postdata();
}

// Hook to display after content
add_action('the_content', function($content) {
    if (is_single()) {
        ob_start();
        display_related_posts();
        $related = ob_get_clean();
        return $content . $related;
    }
    return $content;
});
  1. Set “Code Type” to “PHP Function”
  2. Click “Save Snippets”

This approach is safer than editing theme files directly and you can turn on / off the snippet anytime.

Conclusion

By implementing related posts properly, you can increase page views and engagement for your WordPress site. If you need a technical team for this matter, don’t hesitate to book a delivery call with Kahunam’s experts or drop us a message.

Wave

Enjoy our articles? Join our free list and get more.

Sign Up

Book a Discovery Call