<?php
/**
 * Related posts based on categories and tags.
 * 
 */

$food_critic_blogs_related_post_counts = get_theme_mod( 'food_critic_blogs_related_post_counts', 3 );

$food_critic_blogs_post_args = array(
    'posts_per_page'    => $food_critic_blogs_related_post_counts,
    'orderby'           => 'rand',
    'post__not_in'      => array( get_the_ID() ),
    'ignore_sticky_posts' => 1,
);

$food_critic_blogs_tax_terms = wp_get_post_terms( get_the_ID(), 'category' );
$food_critic_blogs_terms_ids = array();

foreach( $food_critic_blogs_tax_terms as $tax_term ) {
	$food_critic_blogs_terms_ids[] = $tax_term->term_id;
}

$food_critic_blogs_post_args['tag__in'] = wp_get_post_tags( get_the_ID(), array('fields' => 'ids') ); 

if ( !empty($food_critic_blogs_terms_ids) ) {
    $food_critic_blogs_post_args['category__in'] = $food_critic_blogs_terms_ids;
}

$food_critic_blogs_related_posts = new WP_Query( $food_critic_blogs_post_args );
?>

<?php if ( $food_critic_blogs_related_posts->have_posts() ) : ?>
    <div class="related-post">
        <h3><?php echo esc_html(get_theme_mod('food_critic_blogs_related_posts_heading', 'Related Posts')); ?></h3>
        <div class="row">
            <?php while ( $food_critic_blogs_related_posts->have_posts() ) : $food_critic_blogs_related_posts->the_post(); ?>
              <div class="col-xl-4 col-lg-6 col-md-6 col-12">
                <div class="blog-item wow fadeInUp inner-related-post">
                  <?php  
                    the_title(sprintf('<h5 class="post-title"><a href="%s" rel="bookmark">', esc_url(get_permalink())), '</a></h5>');
                  ?>
                  <?php
                    $food_critic_blogs_excerpt_limit = 20;
                    echo "<p>" . wp_trim_words(get_the_excerpt(), $food_critic_blogs_excerpt_limit) . "</p>";
                  ?>
                  <ul class="comment-timing">
                    <li><a href="javascript:void(0);"><i class="fa fa-comment"></i> <?php echo esc_html(get_comments_number($post->ID)); ?></a></li>
                    <li><a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>"><i class="fa fa-user"></i><?php esc_html_e('By', 'food-critic-blogs'); ?> <?php the_author(); ?></a></li>
                    <li><a href="javascript:void(0);"><i class="fas fa-clock pe-1"></i> <?php echo esc_html( get_the_time( 'F j, Y' ) ); ?> <?php echo esc_html( get_the_time( 'H:i A' ) ); ?></a></li>
                  </ul>
                </div>
              </div>
            <?php endwhile; ?>
        </div>
    </div>
  <?php else : ?>
    <p class="related-post"><?php echo esc_html__('Sorry, no related posts found.', 'food-critic-blogs'); ?></p>
<?php endif;

wp_reset_postdata();
?>