Get Related Posts Based On The Post’s Content In WordPress

Trying to get the IDs of related posts based on the content of the post in WordPress? All you may need is to register this custom function to call the related post IDs.

Get related posts based on the post's content

To get the list of similar posts, based on the current post’s content; you must register this function in your WordPress theme’s functions.php file.

function custom_related_posts() {
     $current_id = get_the_ID();
     $current_content = get_the_title( $current_id );
     $similar = array();
     $scores = array();
     $percentage = 0;

     function compare_content( $current_content, $comparison_content ) {
          $current_content = trim( strtolower( $current_content ) );
          $comparison_content = trim( strtolower( $comparison_content ) );
          similar_text( $current_content, $comparison_content, $percentage );
          $formated_percents = number_format( $percentage );
          return $formated_percents;
     }

     $args = array(
          'post_type' => 'post',
          'posts_per_page' => -1
     );
     $loop = new wp_query( $args );
     while( $loop->have_posts() ) {
          $loop->the_post();
          $score = compare_content( $current_content, get_the_title() );
          if ( $score > 55 && get_the_ID() != $current_id ) {
               array_push( $similar, get_the_ID() );
               array_push( $scores, $score );
          }
     }
     wp_reset_query();

     array_multisort( $scores, SORT_NUMERIC, SORT_DESC, $similar );

     if ( empty( $similar ) ) {
          $similar = 2147483647; //unrealistic post ID
     }

     $args = array(
          'post_type' => 'post',
          'posts_per_page' => 5,
          'post__in' => $similar,
          'order_by' => 'ID',
          'order' => 'DESC'
     );
     $loop = new wp_query( $args );
     if ( $loop->have_posts() ) {
          $output = '<h4>Related guides</h4>';
          $output .= '<ul>';
          $i = -1;
          while( $loop->have_posts() ) {
               $loop->the_post();
               $i++;
               $output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a> &ndash; ' . $scores[$i] . '% related guide</li>';
          }
          $output .= '</ul>';
     }
     wp_reset_query();

     return $output;
}

Note that the above function will return a list of matched posts with the match of 55% when called. See the next section on how to match a different percentage of content.

Usage

Simply echo the new custom_related_posts() function in your single-post.php file, located in your WordPress theme’s directory.

<?php echo custom_related_posts(); ?>

Changing the percentage of matched-content

Note, if you’re trying to increase the percentage of matches, simply replace the 55 in the line below of the custom_related_posts(); function.

if ( $score > 55 && get_the_ID() != $current_id ) {

Matching the post's title instead of the content

To match the title instead of the content, simply replace get_the_content() located twice, with get_the_title() in this function.

 $current_content = get_the_title( $current_id );
 $score = compare_content( $current_content, get_the_title() );