<?php
$style = kanews_get_option('related-posts-style') ? kanews_get_option('related-posts-style') : 3; 
if(get_post_meta( $post->ID, 'show_posts_related', true ) != 'off') {
if($style != 'disable'):
  $related_count = kanews_get_option('related-posts-count') ? kanews_get_option('related-posts-count') : 6;
  $sortby = kanews_get_option('related-posts-sortby') ? kanews_get_option('related-posts-sortby') : 'random';
  $tags = wp_get_post_terms( get_queried_object_id(), 'post_tag', ['fields' => 'ids'] );
  $categories = get_the_category($post->ID); if ($categories) {$category_ids = array(); foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;}
  
  $post_not_in = isset($GLOBALS['related_post_not_in']) && $GLOBALS['related_post_not_in'] != '' ? $GLOBALS['related_post_not_in'] : array($post->ID);

  if( $sortby == 'tag' )
  $category_ids = '';
  else
  $tags = '';

  $query_args = array(
    'number'				=> $related_count,
    'cats'    		  => $category_ids,
    'sortby' 				=> $sortby,
    'exclude'    		=> $post_not_in,
    'tags_ids'      => $tags,
  );
  $kanews_query = kan_query( $query_args );

  $row_class = 'row equal-height';
  if($style == '6')
  $row_class = 'row no-gutters';
  if ($kanews_query->have_posts()) : ?>
    <div class="kanews-section-box" id="related-articles">
      <?php echo kanews_widget_title(kanews_translate('related-posts-text',esc_html__( 'Benzer Haberler', 'kanews' )), 'h4', 'kanews-section-flat-heading'); ?>
      <div class="<?php echo $row_class; ?>">
        <?php while($kanews_query->have_posts()): $kanews_query->the_post(); global $post;
          get_template_part('template-parts/archives/article', $style );
        endwhile; wp_reset_postdata(); ?>
      </div>
    </div>
  <?php endif;
endif;
}