News or e-commerce websites, which have multiple blog posts or products, usually need Related Posts. Adding related posts to your posts brings some advantages of SEO and user experience. However, using plugins that create related posts automatically may not satisfy when you want to choose some particularly related posts. In this tutorial, we'll use Meta Box to create related posts that you can choose manually.

What are Related Posts?

Related posts, as its name says, are the posts which have intimate content with the current post. Its content may be a kind of additional information, inline, suggestion, etc.

Related posts are often displayed in a group and put at the end, sidebar, or even in the middle of the post. The number and position of the related posts depend on your website design.

For visitors, having related posts gives them suggestions to read more related content which they may need. In the event the current post does not have enough information, related posts are a good way to get more.

To the website owners, the related posts create or increase a significant number of internal links on your site. It pulls the visitors to stay longer on your site at the same time. This thing helps you improve multiple website indicators such as the bounce rate, page view, time on site, and so on.

Normally, most tools set related posts automatically based on the keywords, tags, categories, or taxonomies. It might be not precise, especially when your marketing team wants particularly related posts to be prior on the list. In that case, choosing related posts manually will be the best way.

Related post is shown at the end of posts - an example from metabox.io
Related posts are shown at the end of posts

What is Meta Box? What is the Post Field?

Meta Box is a light and simple plugin but powerful to create and configure custom fields in WordPress. In case you are not familiar with Meta Box yet, this post is for your reference.

Meta Box supports more than 40 types of custom fields, means that almost the fields everyone needs. Meta Box also supports fields that associate databases from posts, pages, and custom post types in WordPress.

In this post, we are going to use a special field type named Post Field, which allows us to link to posts, to create related posts.

Post Field is a field that allows you to choose one or multiple posts. It may be displayed in several styles as select, checkbox or radio. Using Post Field allows us to choose a particular post, even in different post types, manually.

How to Add Related Posts to WordPress Using Meta Box?

Step 1: Install Meta Box Plugin

Firstly, you need to install and activate the Meta Box plugin on your WordPress site. The plugin is free and available on wordpress.org:

If you are not familiar to PHP or don’t want to touch any line of code, you should install a premium extension from Meta Box in addition, Meta Box Builder. This extension (it is a plugin as well) provides you an intuitive user interface to create and configure custom fields without coding. Even when you have no knowledge of PHP, the execution is really easy. Otherwise, you can save money with the free Online Generator tool.

If you are a developer, you may create fields by code. That means you need to install the Meta Box plugin only.

FYI, here is the instruction to install and activate plugins.

Step 2: Create and Configure Post Field

When you installed Meta Box Builder, creating a Post Field is very easy for everyone.

In the Admin Dashboard, go to the Meta Box → Custom Fields → Add New.

In the displaying screen of Edit Field Group, name your field group in Title field, set it an ID, then click on Update. You have a field group to hold the Post Field now.

Next, click Add Field button and select Post in the drop-down menu. A post field will appear automatically inside the created field group.

Create a post field using Meta Box Builder

Now, we configure the post field. There are some notices about setting in this step:

Description & Post Type:

In this field, you may give some instruction on how to choose the posts in the Lable Description. E.g. I made it be “Choose 3 Related Posts” to request the writers choose enough three posts to be related.

If you have several post types on your site, you must indicate the right post type you want to be related to (pay attention to this, no. 1). For instance, if the related posts you want are in the type of a custom post type, Products, you must choose Products here. If the related post is in the type of default blog posts, you must choose Post here. You can choose more than 1 post type.

choose multiple post type to be related posts

Tip:

With the Post Type option, you absolutely can choose another post type’s posts to be related. e.g products can be related to a blog post.

Take the case of a travel agency that sells tours to go to Thailand where each tour is a product page (its post type is Product). If they have a blog post that shares travel experiences and tips in Thailand, by set post type Product here, they can put some Thailand tours to be related posts to suggest people buy their tours. Such a good way to increase sales!

Field Type:

This field stipulates how the post field show the posts inside. There are several styles to show, ones of the most common styles are:

Related posts show in Select Advance style

Related posts show in Checkbox list style

Related posts show in Checkbox tree style

Personally, I prefer Select Advanced because it is neat and provides searching. If we have a significant number of posts, e.g. over 1,000 posts, scrolling to find a post may be dull. Otherwise, if you set pages to be related (there are few pages), scrolling of Checkbox list or Radio may work.

Display the "Select All | None" button:

Only tick this box when your field is in the Checkbox list style. That makes a “Toggle All” button appears allowing you to choose/unchoose all the posts with one click.

After configuring all options in the Field tab of the Edit Field Group screen, move to the Settings tab. In the Post Types field, choose a post type of the post where you want to show related posts (pay attention to this, no. 2).

Choose a post type of the post where you want to show related posts

Finally, click on Save Changes to save all the settings.

Instead of using Meta Box Builder to create Post Field as above, you may code and take a look at my below code. This is a gist generated automatically by Meta Box Builder plugin after creating Post Field. If you put it into the function.php file of your theme, you will get the same result as mine.



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


<?php
add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
function your_prefix_register_meta_boxes( $meta_boxes ) {
$meta_boxes[] = array (
'title' => 'Related Post',
'id' => 'related-post',
'post_types' => array(
0 => 'post',
),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array (
'id' => 'related_posts',
'type' => 'post',
'name' => 'Related Posts',
'desc' => 'Choose 3 Related Posts',
'post_type' => array(
0 => 'post',
1 => 'page',
),
'field_type' => 'select_advanced',
'multiple' => true,
'required' => 1,
),
),
);
return $meta_boxes;
}
view raw

meta-box.php

hosted with ❤ by GitHub

Step 3: Add Related Posts to Each Post

Now, back to the edit page of your post, which has the post type you chose to show related posts (the attention point no.2). Move to where you place the created field “Related Posts” and click on there. A list of posts will show as following:

A list of posts will show to choose related posts
A list of posts will show to choose

This list includes all the posts in the post types which you chose (attention point no.1). Just click on the post title, then press Update. Now, your post had related posts.

Step 4: Show the Related Posts on the Front End

The created Post Field saves the IDs of chosen related posts. It returns an array of values as follows:

array (size=3)
    0 => string '36' (length=2)
    1 => string '25' (length=2)
    2 => string '29' (length=2)

So, we use this function to get the value of posts:

$post_ids = rwmb_meta( $field_id );
foreach ( $post_ids as $post_id ) {
    echo '<p>'. get_the_title( $post_id ). '</p>';
}

In there:

  • $field_id: ID of the Post Field which we set
  • $post_ids: The array of post IDs that the Post Field returns

We can customize or export data by IDs. It means that you can get any information from the related posts such as title, thumbnail, excerpt to show on your website. I did get all of them (title, thumbnail, and excerpt) in this instruction.

Put the below gist to the single.php file if your post type is post. If your post is another custom post type, create a file named single-{custom_post_type_slug}.php and put the gist into there. Read more about creating a custom post type here.

Source code:



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


<?php
$field_id = 'related_posts';
$related_posts = rwmb_meta( $field_id );
if (isset($related_posts)) { ?>
<div id="relatedPosts" class="related-posts">
<h3 class="related-posts-headline"><em>You might also like</em></h3>
<div class="related-posts-items jp-related-posts-grid">
<?php foreach ($related_posts as $related_post) { ?>
<div class="related-post-item">
<a class="related-post-a" href="<?php echo get_the_permalink($related_post); ?>" title="<?php echo get_the_title($related_post); ?>" rel="nofollow">
<img class="related-post-img" src="<?php echo get_the_post_thumbnail_url($related_post, 'thumbnail'); ?>" alt="<?php echo get_the_title($related_post); ?>">
</a>
<h4 class="related-post-title">
<a class="related-post-a" href="<?php echo get_the_permalink($related_post); ?>" title="<?php echo get_the_title($related_post); ?>" rel="nofollow"><?php echo get_the_title($related_post); ?></a>
</h4>
<p class="related-post-excerpt"><?php the_excerpt($related_post); ?></p>
</div>
<?php } ?>
</div>
</div>
<?php } ?>
view raw

template.php

hosted with ❤ by GitHub

Final Words

That’s how to add related posts to WordPress posts using Meta Box plugin. With this method, you may customize the related post as you want and choose a particular post as well. Hope that it may help you to give your visitor more useful content, increase SEO score as well as sales.

Other case studies you might be interested in

  1. Create A Dynamic Landing Page in WordPress Using Custom Field
  2. Create a Filter to Find Hotels by Location
  3. Create an OTA Website Like Booking.com with Meta Box Plugin - P1: Create a Page to Introduce Hotel Rooms
  4. Create an OTA Website Like Booking.com with Meta Box Plugin - P2: Create Filters on the Archive Page
  5. Create an OTA Website Like Booking.com with Meta Box Plugin - P3: Create Filters for Single Hotel Pages
  6. Create Dynamic Favicon in WordPress using Meta Box plugin
  7. Create Posts Series in WordPress Using Meta Box
  8. Display a User List On the Frontend with Meta Box
  9. Display The Latest Products Section - P2 - Using Meta Box and Elementor
  10. Display The Latest Products Section - P3 - Using Meta Box And Oxygen
  11. How to Add Custom Fields to Display Banners using Meta Box Plugin
  12. How to Add Guest Authors and Guest Posts - Using Meta Box
  13. How to Add Related Posts to WordPress Using Meta Box
  14. How to Build a Hotel Booking Website Using Meta Box - P1
  15. How to Build a Hotel Booking Website Using Meta Box - P2 - Booking Page in Backend
  16. How to Build a Hotel Booking Website Using Meta Box - P4 - Booking Management Page
  17. How to Build a Hotel Booking Website Using Meta Box – P3 – Booking Page for Customer
  18. How to Create a Classified Ads Website using Meta Box
  19. How to create a FAQs page - P5 - Using Meta Box and Breakdance
  20. How to Create a Product Page - P2 - Using Meta Box and Oxygen
  21. How to Create a Product Page - P3 - Using Meta Box and Bricks
  22. How to Create a Product Page - P4 - Using Meta Box and Elementor
  23. How to Create a Product Page - P5 - Using Meta Box and Gutenberg
  24. How to Create a Product Page - P6 -Using Meta Box and Breakdance
  25. How to Create a Product Page - P7 - Using Meta Box + Kadence
  26. How to Create a Product Page - P8 - Using Meta Box and Brizy
  27. How to Create a Product Page using Meta Box Plugin
  28. How to Create a Recipe - P2 - Using Meta Box and Oxygen
  29. How to Create a Recipe - P3 - Using Meta Box and Elementor
  30. How to Create a Recipe - P4 - Using Meta Box and Bricks
  31. How to Create a Recipe - P5 - Using Meta Box and Zion
  32. How to Create a Recipe - P6 - Using Meta Box and Brizy
  33. How to Create a Recipe - P7 - Using Meta Box and Breakdance
  34. How to Create a Recipe with Meta Box Plugin
  35. How to Create a Simple Listing - P2 - Using Meta Box and Bricks
  36. How to Create a Team Members Page - P1- Using Meta Box and Elementor
  37. How to Create a Team Members Page - P2 - Using Meta Box and Oxygen
  38. How to Create a Team Members Page - P3 - Using Meta Box and Bricks
  39. How to Create a Team Members Page - P4 - Just Meta Box
  40. How to Create a Team Members Page - P6 - using Meta Box and Breakdance
  41. How to Create a Video Gallery Page - P2 - Using Meta Box + Bricks
  42. How to Create a Video Gallery Page - P3 - Using Meta Box and Breakdance
  43. How to Create a Video Gallery Page - P4 - Using Meta Box + Elementor
  44. How to Create a Video Gallery Page - P5 - Using MB Views
  45. How to Create a Video Gallery Page Using Meta Box + Oxygen
  46. How to Create ACF Flexible Content Field with Meta Box
  47. How to Create an Auto-Updated Cheat Sheet in WordPress
  48. How to Create an FAQs Page - P1 - Using Meta Box and Elementor
  49. How to create an FAQs page - P2 - Using Meta Box and Oxygen
  50. How to create an FAQs page - P4 - Using Meta Box and Bricks
  51. How to Create an FAQs Page -P3- Using Meta Box
  52. How to Create Buttons with Dynamic Link using Custom Fields
  53. How to Create Category Thumbnails & Featured Images Using Custom Fields
  54. How to Create Download and Preview Buttons - P1 - Using Meta Box and Bricks
  55. How to Create Download and Preview Buttons - P2 - Using Meta Box and Oxygen
  56. How to Create Download Buttons Using Custom Fields with Meta Box Plugin
  57. How to Create Menus for Restaurants - P1 - Using Meta Box and Elementor
  58. How to Create Menus for Restaurants - P2- Using Meta Box and Bricks
  59. How to Create Online Admission Form for School or University
  60. How to Create Online Reservation Form for Restaurants using Meta Box
  61. How to Create Relationships - P1 - Using Meta Box and Oxygen
  62. How to Create Relationships - P2 - Using Meta Box and Bricks
  63. How to Create Relationships - P3 - Using MB Views
  64. How to Create Taxonomy Thumbnails & Featured Images - P2 - Using Meta Box and Oxygen
  65. How to Create Taxonomy Thumbnails & Featured Images - P3 - Using Meta Box and Bricks
  66. How to Display Images from Cloneable Fields - P1 - with Gutenberg
  67. How to Display Images from Cloneable Fields - P2 - with Oxygen
  68. How to Display Images from Cloneable Fields - P3 - with Elementor
  69. How to Display Images from Cloneable Fields - P4 - with Bricks
  70. How to Display Opening Hours for Restaurants - P1 - Using Meta Box + Gutenberg
  71. How to Display Opening Hours for Restaurants - P2 - Using Meta Box and Oxygen
  72. How to Display Product Variations - P1 - Using Meta Box and Gutenberg
  73. How to Display Product Variations - P2 - Using Meta Box and Oxygen
  74. How to Display Product Variations - P3 - Using Meta Box and Bricks
  75. How to Display The Latest Products - P5 - Using Meta Box and Bricks
  76. How to Display the Latest Products - P6 - using Meta Box and Breakdance
  77. How to Display the Latest Products - P7 - Using Meta Box + Kadence
  78. How to Display the Latest Products Section - P4 - Using Meta Box + Zion
  79. How to Display the Most Viewed Posts - P1 - using MB Views
  80. How to Display the Most Viewed Posts - P2 - using Meta Box and Oxygen
  81. How to Display the Most Viewed Posts - P3 - Using Meta Box and Bricks
  82. How to Filter Posts by Custom Fields - P2 - using Meta Box and FacetWP
  83. How to Manually Reorder Posts with Meta Box
  84. How to Show Featured Restaurants on Homepage - P1 - Meta Box + Elementor + WP Grid Builder
  85. How to Show Posts With a Specific Criteria - P3 - Using MB Views
  86. How to Show Posts with Specific Criteria - P1 - Using Meta Box and Bricks
  87. How to Show Posts with Specific Criteria - P2 - Using Meta Box and Oxygen
  88. How to Show Posts with Specific Criteria - P4 - Using Meta Box + Breakdance
  89. How to Show Posts with Specific Criteria - P5 - Using Meta Box and Elementor
  90. How to Show the Featured Restaurants - P3 - using Meta Box and Oxygen
  91. How to Show the Featured Restaurants - P4 - Using MB Views
  92. How to Show the Featured Restaurants - P5 - Using Meta Box and Elementor
  93. How to Show the Featured Restaurants - P6 - Using Meta Box and Zion
  94. How to Show the Featured Restaurants Section - P2 - Using Meta Box and Bricks

2 thoughts on “How to Add Related Posts to WordPress Using Meta Box

  1. I have set my posts to display recent posts from the post category. When I have a post with more than one category, I get 2 instances of recent posts. Is there a way to tell the plugin to limit to the first category instance? thanks!

Leave a Reply to Andy Globe Cancel reply

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