The following code must be inserted in the function.php contained in your child theme (**).

 

function related_posts_shortcode( $atts ) {
    extract(shortcode_atts(array(
      'limit' => '5',
  ), $atts));
     
    global $wpdb, $post, $table_prefix;
     
    if ($post->ID) {
      $retval = '
    '; // Get tags $tags = wp_get_post_tags($post->ID); $tagsarray = array(); foreach ($tags as $tag) { $tagsarray[] = $tag->term_id; } $tagslist = implode(',', $tagsarray); // Do the query $q = "SELECT p.*, count(tr.object_id) as count FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p WHERE tt.taxonomy ='post_tag' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID AND p.post_status = 'publish' AND p.post_date_gmt get_results($q); if ( $related ) { foreach($related as $r) { $retval .= '
  • '.wptexturize($r->post_title).'
  • '; } else { $retval .= '
  • No related posts found
  • '; } $retval .= '
'; return $retval; } return; } add_shortcode('related_posts', 'related_posts_shortcode');

 

(**) How to Create a Child Theme

A child theme is a theme that inherits the functionality and styling of another theme, called the parent theme. Child themes are the recommended way of modifying an existing theme.

  1. Create child theme directory (common approach to name child theme as the parent theme plus -child added on the end.)
  2. Create style.css
  3. Create functions.php

For more detailed instructions on child themes you can visit the following links:

https://developer.wordpress.org/themes/advanced-topics/child-themes/

https://blog.hubspot.com/website/wordpress-create-child-theme