<?php

add_action('wp_ajax_load_posts_faq', 'load_posts_faq');
add_action('wp_ajax_nopriv_load_posts_faq', 'load_posts_faq');

function load_posts_faq() {
	check_ajax_referer('load_more_faq', 'security');
	$paged = $_POST['page'];
	$category = $_POST['category'];
	$offset = $paged * 6 - 3;
	if($category === 'all'):
		$args = array(
			'post_type' => 'faq',
			'post_status' => 'publish',
			'posts_per_page' => 6,
			'paged' => $paged,
            'offset' => $offset
		);
    elseif($category === 'media'):
        $args = array(
			'post_type' => 'faq',
			'post_status' => 'publish',
			'posts_per_page' => 6,
			'paged' => $paged,
            'offset' => $offset,
            'tax_query' => array(
                array(
                    'taxonomy' => 'post_format',
                    'field' => 'slug',
                    'terms' => array( 'post-format-video' ),
                    'operator' => 'IN'
                )
            )
		);
    else:
    $args = array(
        'post_type' => 'faq',
		'post_status' => 'publish',
		'posts_per_page' => 6,
		'paged' => $paged,
		'offset' => $offset,
		'tax_query' => array(
			array(
				'taxonomy' => 'create_taxonomy_faq',
				'field' => 'slug',
				'terms' => $category,
			)
		)
	);
	endif;
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ):
		?>
	<?php while ( $loop->have_posts() ) : $loop->the_post();?>
		<?php
			$post_id = get_the_ID();
			$imagefeatured = get_the_post_thumbnail_url($post_id,'list-archive');
			$format = get_post_format( $post_id );
			if(!$format):
				$format = "article";
			endif;
		?>
		<div class="col-md-6 col-lg-4">
			<div class="c-featured-news">
				<a href="<?php the_permalink(); ?>">
					<img src="<?php echo $imagefeatured; ?>" alt="<?php echo the_title(); ?>">
					<div class="c-card-news__type"><i class="icon icon-<?php echo $format; ?>"></i></div>
				</a>
			</div>
			<article class="c-card-category">
				<a href="<?php the_permalink(); ?>">
					<h3 class="c-card-category__title mt-1"><?php echo the_title(); ?></h3>
				</a>
			</article>
		</div>
<?php
	endwhile; wp_reset_query();
	endif;
 
    wp_die();
}