Simplest Method of Getting Related Posts

There’s a lot of plugins available for this purpose but they do go quite deep when it comes to finding related posts. Here we’ll go over a simpler method, by getting posts from within the same categories.

The Query

For category__in we use the categories of the current post, we make sure the current post is excluded with post__not_in and we make it a random order with orderby.

The reason for ‘no_found_rows’ => true is to speed up the query a bit, we’re not using pagination so no need for that.

Date Limitation

If you have a lot of posts then we can improve it a bit by excluding older posts. For example this is the code to exclude posts older than 1 year.

Posts With Featured Images

Generally the way related posts are displayed is with a featured image, so we can make sure to exclude posts that do not have a featured image:

Final Words

That’s all, quite a simple approach to get related posts.

Tags: ,
Previous Post
get-image-alt-using-wordpress
WPDev101

Get Image ALT Attribute Using Attachment ID

Next Post
enable-gutenberg-on-custom-post-type
WPDev101

Enable Gutenberg on Custom Post Types

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.