WordPress related posts without plugin

WordPress related posts basics

If you are reading this post, then perhaps you are wondering how to display WordPress related posts without a plugin. That task is actually not that hard to be accomplished. In the following guide I will show you two approaches to find and display related posts with just little pieces of custom code.

Before starting this training session, I would like to take a moment and explain that related articles are extremely useful for the visitors. This way they find more and valuable information. If the landing page offers related posts on the matter, then you are naturally increases the chances to lower the bounce rate and keep the visitors longer on your page.

Show wordpress related posts using a custom code

Displaying related posts without plugin keeps your WordPress clean of extra extensions and features that you may not want to use. The pieces of custom code I prepared for you do exactly what you need without any additional options or database queries that may slow down the overall performance of your website.

The first piece of code is designed for single or posts pages. It uses global variable $post and using wp_get_post_tags function gets post ID. Then based on the results it also gets tags based on certain parameters and finally loops through the results. Before proceeding however, I would like to provide more information on the preparations I did before applying my code:

I posted a few articles

I set tags to 3 of them

I checked my post ID. This could be done from wp-admin section -> Posts -> Tags -> Edit next to the Tags -> In the URL you will see a string that says tag_ID= and then a number. In my case this number is 16.

To check if my code is working fine I will use the following snippet:

echo '<pre>';
print_r($variabe);
echo '</pre>';

Its idea is to dump the information stored in the variable so that I can easily determine whether the code is getting and returning the information I need in order to display related posts.

Custom code – revision and explanation

Now let’s start with the code. Right after the PHP opening tag we define our post as equal to $post and also use the global variable. Then we add a new function $wppGetTags which is equal to wp_get_post_tags. It retrieves tags of a specific post. The whole information will be stored in $wppGetTags. Here is the first point where we can make a little test. If you add the following code:

echo '<pre>';
print_r($wppGetTags);
echo '</pre>';

after:

$wppGetTags = wp_get_post_tags($post->ID);

you will see the following output in your single or posts page:

WordPress related posts – data dump

As you can see the term_id is 16. Name and slug are mytag. Taxonomy and its ID are post_tag and again the number 16. Count is 3. This number comes from posts_per_page parameter which we passed to our args array and finally the filter is raw. If you see similar results on your page, then the test is passed successfully and we are ready to proceed further. Now let’s comment or remove these 3 additional lines and continue with our code.

So far we identified our post, its ID and tags. The 4th lines says if $wppGetTags returns some information go to the 5th line and define a new variable $tagIds which is equal to an array. That array contains foreach loop that loops through the results stored in $wppGetTags. We also pass arguments to it. We say, we want all tags related tags, that are not in this posts and we want the maximum number of the tags displayed to be 3. We also say caller_get_post equal to 1, because we do not want any sticky posts from being displayed first.

At this moment we have prepared our arguments and now we are ready to start looping through posts and display the titles of the related posts that are actually links to these posts. To do so we define a new variable $my_query which is a new instance of wp_query and pass our arguments to it. At the end we return $post and also reset our query with wp_reset_query(). This is important part as we do not want to mess with the standard WordPress query. If you miss to reset, then your WordPress instance may start acting strangely and return unpredictable results.

Custom code additions

Before checking the other approach I would like to say that I intentionally did not add fallback message for the first if statement. Let’s looks at line number 4. There we say if we have some information stored in $wppGetTags, then proceed with the code. Generally speaking it would be good to add else statement which would say something like “ There are no related posts”, but I skipped it as I do not want to see such message while working on my code. I however, will add it in the second piece of code.

Here is the final result of this code. Note it should be added to single.php file:

WordPress related posts on signle page

The second piece of code would also help you display wordpress related articles. It is however, designed to act differently. Instead of modifying single or posts files, we will work directly with functions.php. We will user action hook, execution point and again custom function to identify the related posts. Now let’s start with it.

The construction of the action hook is very simple. It says in the end of each loop trigger our custom function (custom_wpp_related_posts). The code of the function is also similar to the first piece of code. We again get post tags and loop through them. Our arguments define the slug, number of posts and not it. As I already said, I do not want the current post to appear in the related posts, so I again added post not in to our array. After defining our arguments we define to if statements. We check if we have some data in wpp_related_posts variable and also check if the page we are on is singular. This second is an extra check because I do not want related posts to appear on category or archive pages.

To make the code a little bit more interesting we use setup_postdata for the formatting in our foreach loop. To avoid switch PHP and HTML we directly echo out out HTML code into PHP. For better control we also define class for our posts and also add else statement for the first if statement. It says No related posts were found and this is our fallback message if our code does not find any related articles. In the end we again reset our function. As we used setup_postdata, we do this with wp_reset_postdata. This way we can be sure that the WordPress functionality is kept intact.

We are not industry specific. We are WordPress specific. We work with everyone to help find solutions for the troubles.
If you show us the problem you are experiencing, we will show you how to fix it. It is that simple.

Easy WordPress automation

Switching from human control to automation is an essential step in increasing the efficiency of website development or maintenance. During the past decade, WordPress advanced a lot and became a fully functional Content Management System. Thanks to some of its internal features a large portion of its functionality could be easily automated. The following articles give ideas on how to apply successful automation:

Speed up WordPress

Speed is an important matter when it comes to website success. In order to appear on the first page except for applying the well-known SEO techniques, you will also have to pay special attention to the performance of your website. We have reviewed a number of options and prepared articles for the most important aspects that include database management and dealing with different types of content:

Extend WordPress features

Extending the standard WordPress functionality by building new features was always a hot topic. Developing themes that provide next-level User Experience could also be described similarly. Thanks to the established standards WordPress developers could create standardized extensions that can be downloaded from the official repository. In the following articles, we described some simple techniques that would help start developing your own extensions: