wp query – Include current post into loop

Question

I have a related posts loop ( in every single post page ) based on post title. source : https://wpza.net/get-related-posts-based-on-the-posts-content-in-wordpress/

But I also need to include current post into this loop 'post__in' => $similar, . Please can anyone help me how can I do this

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

     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' => 'product',
          '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 > 90 && 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 ) ) {
          $args = array(
               'post_type' => 'product',
               'posts_per_page' => 100,
               'post__in' => $similar,
               'orderby' => 'meta_value_num',
'order' => 'asc',
'meta_key' => '_price'
          );
          $loop = new wp_query( $args );
          $i = -1;
          while ( $loop->have_posts() ) {
               $loop->the_post();
               $i++;
               echo do_shortcode('[wbcr_snippet id="22379"]') ; echo  '<div class="ai">'. $scores[$i] .'% oxsar mehsul</div>'; 
          }
          wp_reset_query();
     }
     return $output;
}

0
Teymur Abbasov 4 months 2022-04-08T05:23:30-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse