title: How to display related posts from this code?
body: <p>I have two scripts to display related posts that fit my terms, but I don't know how to call them, please help me. I hope someone helps and provides the complete script because this code complies with the rules I expect.</p>
  
  <p>I found this code <a href="https://wordpress.stackexchange.com/questions/98024/related-posts-by-multiple-tags">here</a>.</p>
  
  <p><strong>Related Posts 1:</strong> How to call related posts from this code?</p>
  
  <pre><code>function get_pew_related_data($args, $post_id, $related_id) {
      global $post, $wpdb;
      $post_id = intval( $post_id );
      if( !$post_id &amp;&amp; $post-&gt;ID ) {
          $post_id = $post-&gt;ID;
      }
  
      if( !$post_id ) {
          return false;
      }
  
      $defaults = array(
          'taxonomy' =&gt; 'topics',
          'post_type' =&gt; array('post'),
          'max' =&gt; 5
      );
      $options = wp_parse_args( $args, $defaults );
  
      $transient_name = 'pew-related-' . $options['taxonomy'] . '-' . $post_id;
  
      if( isset($_GET['flush-related-links']) &amp;&amp; is_user_logged_in() ) {
          echo '&lt;p&gt;Related links flushed! (' . $transient_name . ')&lt;/p&gt;';
          delete_transient( $transient_name );
      }
  
      $output = get_transient( $transient_name );
      if( $output !== false &amp;&amp; !is_preview() ) {
          //echo $transient_name . ' read!';
          return $output;
      } 
  
      $args = array(
          'fields' =&gt; 'ids',
          'orderby' =&gt; 'count',
          'order' =&gt; 'ASC'
      );
      $orig_terms_set = wp_get_object_terms( $post_id, $options['taxonomy'], $args );
  
      //Make sure each returned term id to be an integer.
      $orig_terms_set = array_map('intval', $orig_terms_set);
  
      //Store a copy that we'll be reducing by one item for each iteration. 
      $terms_to_iterate = $orig_terms_set;
  
      $post_args = array(
          'fields' =&gt; 'ids',
          'post_type' =&gt; $options['post_type'],
          'post__not_in' =&gt; array($post_id),
          'posts_per_page' =&gt; 50
      );
      $output = array();
      while( count( $terms_to_iterate ) &gt; 1 ) {
  
          $post_args['tax_query'] = array(
              array(
                  'taxonomy' =&gt; $options['taxonomy'],
                  'field' =&gt; 'id',
                  'terms' =&gt; $terms_to_iterate,
                  'operator' =&gt; 'AND'
              )
          );
  
          $posts = get_posts( $post_args );
  
          /*
          echo '&lt;br&gt;';
          echo '&lt;br&gt;';
          echo $wpdb-&gt;last_query;
          echo '&lt;br&gt;';
          echo 'Terms: ' . implode(', ', $terms_to_iterate);
          echo '&lt;br&gt;';
          echo 'Posts: ';
          echo '&lt;br&gt;';
          print_r( $posts );
          echo '&lt;br&gt;';
          echo '&lt;br&gt;';
          echo '&lt;br&gt;';
          */
  
          foreach( $posts as $id ) {
              $id = intval( $id );
              if( !in_array( $id, $output) ) {
                  $output[] = $id;
              }
          }
          array_pop( $terms_to_iterate );
      }
  
      $post_args['posts_per_page'] = 10;
      $post_args['tax_query'] = array(
          array(
              'taxonomy' =&gt; $options['taxonomy'],
              'field' =&gt; 'id',
              'terms' =&gt; $orig_terms_set
          )
      );
  
      $posts = get_posts( $post_args );
  
      foreach( $posts as $count =&gt; $id ) {
          $id = intval( $id );
          if( !in_array( $id, $output) ) {
              $output[] = $id;
          }
          if( count($output) &gt; $options['max'] ) {
              //We have enough related post IDs now, stop the loop.
              break;
          }
      }
  
      if( !is_preview() ) {
          //echo $transient_name . ' set!';
          set_transient( $transient_name, $output, 24 * HOUR_IN_SECONDS );
      }
  
      return $output;
  }
  
  function pew_related( $args = array(), $post_id = '', $related_id = '' ) {
      $post_ids = get_pew_related_data( $args, $post_id, $related_id );
  
      if( !$post_ids ) {
          return false;
      }
  
      $defaults = array(
          'post__in' =&gt; $post_ids,
          'orderby' =&gt; 'post__in',
          'post_type' =&gt; array('post'),
          'posts_per_page' =&gt; min( array(count($post_ids), 10)),
          'related_title' =&gt; 'Related Posts'
      );
      $options = wp_parse_args( $args, $defaults );
  
      $related_posts = new WP_Query( $options );
      if( $related_posts-&gt;have_posts() ):
      ?&gt;
      &lt;h5&gt;&lt;?=$options['related_title']?&gt;&lt;/h5&gt;
      &lt;div id="related-material" class="promo"&gt;
          &lt;?php while ( $related_posts-&gt;have_posts() ):
              $related_posts-&gt;the_post();
          ?&gt;
              &lt;a class="post" href="&lt;?=the_permalink();?&gt;"&gt;
                  &lt;div class="meta"&gt;
                      &lt;?php
                      $post_project = wp_get_object_terms($related_posts-&gt;post-&gt;ID, 'projects');
                      $project = 'Pew Research Center';
                      $project_slug = '';
                      if( isset($post_project[0]) ) {
                          $project = $post_project[0]-&gt;name;
                          $project_slug =  $post_project[0]-&gt;slug;
                      } elseif( $related_posts-&gt;post-&gt;post_type == 'fact-tank' ) {
                          $project = 'Fact Tank';
                          $project_slug = 'fact-tank';
                      }
                      ?&gt;
                      &lt;span class="project &lt;?=$project_slug;?&gt; right-seperator"&gt;&lt;?=$project;?&gt;&lt;/span&gt;
                      &lt;span class="date"&gt;&lt;?php the_time('M j, Y'); ?&gt;&lt;/span&gt;
                  &lt;/div&gt;
                  &lt;h2&gt;&lt;?=the_title();?&gt;&lt;/h2&gt;
              &lt;/a&gt;
          &lt;?php endwhile;
              wp_reset_postdata();
  
           ?&gt; 
      &lt;/ol&gt;
      &lt;/div&gt;
      &lt;?php
      endif;
  
  }
  </code></pre>
  
  <p><strong>Related Posts 2:</strong> How to call related posts from this code?</p>
  
  <pre><code>function exe_get_related_posts_by_common_terms( $post_id, $number_posts = 0, $taxonomy = 'post_tag', $post_type = 'post' ) {
      global $wpdb;
  
      $post_id = (int) $post_id;
      $number_posts = (int) $number_posts;
  
      $limit = $number_posts &gt; 0 ? ' LIMIT ' . $number_posts : '';
  
      $related_posts_records = $wpdb-&gt;get_results(
          $wpdb-&gt;prepare(
              "SELECT tr.object_id, count( tr.term_taxonomy_id ) AS common_tax_count
               FROM {$wpdb-&gt;term_relationships} AS tr
               INNER JOIN {$wpdb-&gt;term_relationships} AS tr2 ON tr.term_taxonomy_id = tr2.term_taxonomy_id
               INNER JOIN {$wpdb-&gt;term_taxonomy} as tt ON tt.term_taxonomy_id = tr2.term_taxonomy_id
               INNER JOIN {$wpdb-&gt;posts} as p ON p.ID = tr.object_id
               WHERE
                  tr2.object_id = %d
                  AND tt.taxonomy = %s
                  AND p.post_type = %s
               GROUP BY tr.object_id
               HAVING tr.object_id != %d
               ORDER BY common_tax_count DESC" . $limit,
              $post_id, $taxonomy, $post_type, $post_id
          )
      );
  
      if ( count( $related_posts_records ) === 0 )
          return false;
  
      $related_posts = array();
  
      foreach( $related_posts_records as $record )
          $related_posts[] = array(
              'post_id' =&gt; (int) $record-&gt;object_id,
              'common_tax_count' =&gt; $record-&gt;common_tax_count
          );
  
      return $related_posts;
  }
  </code></pre>