How to Add a Related Posts Feature to Your WordPress Site

Adding a related posts feature to your WordPress site is an excellent way to increase user engagement and keep your visitors on your site for longer periods. By presenting them with articles similar to the one they’re currently reading, you encourage them to explore your content further. In this article, we’ll discuss how to add a related posts feature to your WordPress site using both plugins and manual methods.

Option 1: Using a Related Posts Plugin

The easiest way to add related posts to your WordPress site is by using a plugin. There are several options available, but we’ll focus on two popular choices: Yet Another Related Posts Plugin (YARPP) and Contextual Related Posts.

  1. Yet Another Related Posts Plugin (YARPP)

YARPP is a powerful and highly customizable related posts plugin that allows you to display related posts based on a unique algorithm that considers post titles, content, and tags.

To install and set up YARPP, follow these steps:

  • a. Go to your WordPress dashboard, click on “Plugins,” and select “Add New.”
  • b. Search for “Yet Another Related Posts Plugin” and click “Install Now.”
  • c. Once installed, click “Activate.”
  • d. Navigate to “Settings” > “YARPP” to configure the plugin according to your preferences.
  1. Contextual Related Posts

Contextual Related Posts is another popular plugin that generates a list of related posts based on the content of your articles. This plugin also allows you to display related posts with thumbnails and customize the output.

To install and set up Contextual Related Posts, follow these steps:

  • a. Go to your WordPress dashboard, click on “Plugins,” and select “Add New.”
  • b. Search for “Contextual Related Posts” and click “Install Now.”
  • c. Once installed, click “Activate.”
  • d. Navigate to “Settings” > “Contextual Related Posts” to configure the plugin according to your preferences.

Option 2: Manually Adding Related Posts Using Tags

If you prefer not to use a plugin, you can manually add related posts to your WordPress site by creating a custom function that queries posts with similar tags. Here’s how to do it:

  • a. In your WordPress dashboard, go to “Appearance” > “Theme Editor.”
  • b. Locate the “functions.php” file and click on it to edit.
  • c. Add the following code to the bottom of the file:
function show_related_posts() {
    $tags = wp_get_post_tags(get_the_ID());
    if ($tags) {
        $tag_ids = array();
        foreach ($tags as $tag) {
            $tag_ids[] = $tag->term_id;
        }
        $args = array(
            'tag__in' => $tag_ids,
            'post__not_in' => array(get_the_ID()),
            'posts_per_page' => 4,
            'ignore_sticky_posts' => 1
        );
        $related_posts = new WP_Query($args);
        if ($related_posts->have_posts()) {
            echo '<h3>Related Posts</h3><ul>';
            while ($related_posts->have_posts()) {
                $related_posts->the_post();
                echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
            }
            echo '</ul>';
            wp_reset_query();
        }
    }
}
  • d. Save the changes to the “functions.php” file.
  • e. Locate the “single.php” file and click on it to edit.
  • f. Insert the following code where you’d like the related posts to appear:
<?php show_related_posts(); ?>
  • g. Save the changes to the “single.php” file.

With these changes in place, your WordPress site will now display a list of related posts based on the tags associated with the current post. You can customize the number of related posts displayed by changing the ‘posts_per_page’ value in the ‘show_related_posts’ function.

Option 3: Manually Adding Related Posts Using Categories

Another manual method to display related posts is by using categories. Here’s how to add related posts based on categories:

  • a. In your WordPress dashboard, go to “Appearance” > “Theme Editor.”
  • b. Locate the “functions.php” file and click on it to edit.
  • c. Add the following code to the bottom of the file:
function show_related_posts_by_category() {
    $categories = get_the_category(get_the_ID());
    if ($categories) {
        $category_ids = array();
        foreach ($categories as $category) {
            $category_ids[] = $category->term_id;
        }
        $args = array(
            'category__in' => $category_ids,
            'post__not_in' => array(get_the_ID()),
            'posts_per_page' => 4,
            'ignore_sticky_posts' => 1
        );
        $related_posts = new WP_Query($args);
        if ($related_posts->have_posts()) {
            echo '<h3>Related Posts</h3><ul>';
            while ($related_posts->have_posts()) {
                $related_posts->the_post();
                echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
            }
            echo '</ul>';
            wp_reset_query();
        }
    }
}
  • d. Save the changes to the “functions.php” file.
  • e. Locate the “single.php” file and click on it to edit.
  • f. Insert the following code where you’d like the related posts to appear:
<?php show_related_posts_by_category(); ?>
  • g. Save the changes to the “single.php” file.

Now, your WordPress site will display related posts based on the categories associated with the current post. As with the tags method, you can customize the number of related posts displayed by changing the ‘posts_per_page’ value in the ‘show_related_posts_by_category’ function.

Conclusion

Adding a related posts feature to your WordPress site can significantly improve user engagement and encourage visitors to explore more of your content. Whether you choose to use a plugin or manually add related posts using tags or categories, implementing this feature is an excellent way to boost your site’s performance and keep readers coming back for more.