How To Build Your Own Related Posts Plugin On Genesis

Having a related post plugin and feature at the end of any blog post is a great way to reduce bounce rate and increase readership. Yet Another Related Post (YARP) and nRelate are two of the very popular plugins for displaying related post but they have one serious downside; These related post plugins takes up quite database intensive.

Almost all “Related Posts” plugins suffer from the same fundamental problems regarding MySQL, indexing and search. All of these problems make the plugins themselves extremely database intensive ~ WP Engine

This whole idea of displaying related posts without a plugin comes from the quote above and I was determine to ditch nRelate plugin and enable related posts manually. Yes, you are going to hard code some scripts into your WordPress installation.

In order for us to start, here are a few things you need to get your hands on from your child theme:

  • functions.php
  • style.css

Yup, you got it right! All you need is to place some code on functions.php and them the related posts look prettier using style.css.

Important notes!

Even before doing this WordPress customization, you need to decide if you want the codes to work according to categories, post tags or both.

The below are some simple explanations:

Related posts based on categories – Posts shown are only from the same category. This is a great option if you have many categories and looking to share highly related posts only.

Related posts based on tags – Posts shown are only from the same related tags. This is highly recommended if you are a blogger who focus on very specific tags (great for niche bloggers).

Related posts based on both categories and tags – I had to temporary remove it due to there are some unforeseen issue using it. Will continue to update this once the code is fully baked.

If done correctly, your related theme will look something like this:

How do you enable related posts without plugin on Genesis theme?

Display related posts based on category using Genesis Framework

Before doing any changes to your WordPress files, it is highly recommended to keep a backup copy on your computer in case of any unwanted issue.

Open your cPanel and head over to File Manager to open up your wp-content folder. Alternatively, you may use tools like FileZilla or CyberDuck to perform the changes.

functions.php is located in wp-content folder.

If you are using Genesis Framework, do not perform changes in your core Genesis Framework file. Instead, perform changes in the child theme itself.

The path is roughly as below:

cPanel > File Manager > wp-content > themes > child theme > functions.php

Download or edit functions.php file and place this code at the bottom:

/** Display related posts in Genesis based on Category */
function related_posts_categories() {
if ( is_single ( ) ) {
global $post;
$count = 0;
$postIDs = array( $post->ID );
$related = '';
$cats = wp_get_post_categories( $post->ID );
$catIDs = array( );{
foreach ( $cats as $cat ) {
$catIDs[] = $cat;
}
$args = array(
'category__in' => $catIDs,
'post__not_in' => $postIDs,
'showposts' => 5,
'ignore_sticky_posts' => 1,
'orderby' => 'rand',
'tax_query' => array(


array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-link',
'post-format-status',
'post-format-aside',
'post-format-quote' ),
'operator' => 'NOT IN'
)
)
);
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) {
while ( $cat_query->have_posts() ) {

$cat_query->the_post();
$related .= '<li><a href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to' . get_the_title() . '">' . get_the_title() . '</a></li>';
}
}
}
if ( $related ) {
printf( '<div><h3>Related Posts</h3><ul>%s</ul></div>', $related );
}
wp_reset_query();
}
}
add_action( 'genesis_after_post_content', 'related_posts_categories' );

If you are using Genesis 2.0 or HTML5 ready themes, you have to replace ‘genesis_after_post_content‘ with ‘genesis_after_entry_content‘ on the last line of the code.

Save the file and upload back to your WordPress database. If you are using any cache plugin, purge any cache and clear your browser too.

You should be able to see related posts visible after the blog post now!

Enable related posts without plugin  and based on tags

Just like the above, it is highly recommended that you perform a backup for your functions.php file before doing any changes.

The below is the common path to find your functions.php file:

cPanel > File Manager > wp-content > themes > child theme > functions.php

Place this code at the bottom of your functions.php file:

/** Display related posts in Genesis based on Tags */
function related_posts_tags () {
if ( is_single ( ) ) {
global $post;
$count = 0;
$postIDs = array( $post->ID );
$related = '';
$tags = wp_get_post_tags( $post->ID );
foreach ( $tags as $tag ) {
$tagID[] = $tag->term_id;
}
$args = array(
'tag__in' => $tagID,
'post__not_in' => $postIDs,
'showposts' => 5,
'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-link',
'post-format-status',
'post-format-aside',
'post-format-quote'
),
'operator' => 'NOT IN'
)
)
);
$tag_query = new WP_Query( $args );
if ( $tag_query->have_posts() ) {
while ( $tag_query->have_posts() ) {
$tag_query->the_post();
$related .= '<li><a href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to' . get_the_title() . '">' . get_the_title() . '</a></li>';
$postIDs[] = $post->ID;
$count++;
}
}
if ( $related ) {
printf( '<div><h3>Related Posts</h3><ul>%s</ul></div>', $related );
}
wp_reset_query();
}
}
add_action( 'genesis_after_post_content', 'related_posts_tags' );

If you are using Genesis 2.0 or HTML5 ready themes, you have to replace ‘genesis_after_post_content‘ with ‘genesis_after_entry_content‘ on the last line of the code.

Save the file, upload it to your WordPress database and the last step is to clear all your cache.

Additional tweaks and customization to your related post query

1. Adjusting the number of visibly posts

By default, the code will show up to 5 related posts. If you want to change it, search for the below:

‘showposts’ => 5

Change the digit to any number of your choice.

2. Customizing the fonts and style

Thinking of customizing the related post column? Fire up your style.css and paste the code below:

.related-posts {
margin: 10px 0;
}
.related-posts h3 {
font-size: 18px;
}
.related-posts ul {
list-style:none;
}
.related-posts ul li {
padding: 3px 0;
border-bottom: 1px dashed #ccc;
}
.related-posts ul li a{
font-size:14px;
text-decoration:none;
}

The above is just a sample styling code that you can use. Feel free to change the code to fit your style.

3. Changing the ‘Related Posts’ to better better call-to-actions (CTA)

Changing the wording is simple and all you need to do is to search for this line of code in the functions.php:

printf( '<div><h3>Related Posts</h3><ul>%s</ul></div>', $related );

Now, change the ‘Related Posts’ to any words or CTA of your choice.

Pretty easy right?

Will you stop using related post plugins and hard code it yourself instead?

Now, tell me … will you will hard code related post for your blog or reduce the trouble and use a plugin instead? For me, I would go for hard coding as it will not only reduce the number of plugins used and at the same time, reduce the the server load.

Like this article? Please share it with your friends!

Reginald Chan

Reginald Chan

Professional Business Consultant
Reginald started blogging in 2005 and it soon went from just a hobby to a serious occupation. He specializes in content writing, marketing, social media and SEO. He also tweaks WordPress during his free time. Reginald is also offering paid services and if you need any help, feel free to contact him and check out the services he is offering or check out his latest project, Social Media Rush - Social Media Done Right.
Reginald Chan

@MarketingTweaks

Top 100 #Bloggers on Triberr, @HootsuiteAPAC (MY) Ambassador, I de-mystify social media and online marketing for business owners | #Social #Media #Marketing
*How To Find Laser Target On Twitter?* Twitter is one of the best social media platforms when it comes to finding... http://t.co/rAZ1T6AOiH - 5 mins ago
Reginald Chan
Reginald Chan

Latest posts by Reginald Chan (see all)

Headline Name: Email: subscribed: 210 We respect your privacy Email Marketingby GetResponse

Our Sponsors

  • http://www.mayura4ever.com Mayura

    Hi Reginald,

    Interesting indeed! :)

    Bloggers are now aware of how plugins may affect to their blog performance and yet they can’t eliminate all of ‘em. You know, we need ‘em :)

    Related plugin is MUST have for almost all bloggers and developers have limitations as they should be compatible with as much as possible.

    This is really nice how they can get rid of a plugin in Genesis framework and how it can be vaccinated into the core. Just a matter of copy / paste ;)

    Have a great weekend ahead mate!

    Cheers…

    • http://reginaldchan.net Reginald Chan

      Hi Mayura!

      Thanks for commenting mate. Gratz for being the first guy to comment after reactivating CommentLuv. Haha! Yes, I am always very tired of the plugin which is draining my resources. So, yup!

      Manual coding for the win!

      Have a great day ahead!

      *How have you been?

  • http://www.blogingrace.com/ vijesh

    Hi Reginald,

    This tutorial is a good one for those who have been struggling with recent posts using plugins. Genesis is gaining so popularity due to the fact that its design and quality is superb compared to thesis. Thanks for this tutorial…

    • http://reginaldchan.net Reginald Chan

      Hi Vijesh,

      Thanks for commenting mate! Genesis has always been a great leader when it comes to premium themes. Thesis is good as well but I feel it is tough for beginners who have very limited skills and knowledge in IT.

      Being said that, I would always recommend Genesis over Thesis when it comes to preference.

      Have a great weekend!

  • Pakhi

    Thank you Reginald,
    That was mighty kind of you and I’m sure it’s going to be very helpful.
    Thanks for sharing

    • http://reginaldchan.net Reginald Chan

      Hi Pakhi,

      Certainly hope you find it useful and thanks for dropping by!

      Talk to you soon.

  • http://www.kulerthemes.com/ Stephan

    Very nice article, Reggy,
    It been a while i haven’t visited your blog. How’re you doing?
    All the steps are really easy and clearly to follow up even for beginners thought.
    Genesis can help users to gain popularity more effectively due to their design and post’s quality.
    Thanks for your sharing and hope you doing great.

    Stephan

    • http://reginaldchan.net Reginald Chan

      Hey Stephan,

      I am good thanks. How about you?

      Glad you find the tutorial simple and easy to follow. Haha! I myself had some issues following some others so I decided to create mine personal one. Call me lazy :)

      Have a great day ahead!

  • http://www.SmartInternetLifestyle.com/ Reginald Chan Xin Yon

    Thanks!

    Glad you find this useful man!

  • Steve

    Many thanks for the excellent tutorial. What would I need to add to your code to display each related post in a block with a thumbnail above it? The way nRelate does?

    • http://www.SmartInternetLifestyle.com/ Reginald Chan

      It would work pretty close to what nRelate does. So yes :)