<?php

function listPosts() {
    ?>
     <?php
        $category = $_GET['category'];
        if(!$category) {
            $category = 'all';
        }
        if($category === 'all'):
        $args = array(
            'post_type'      => 'faq',
            'posts_per_page' => 6,
            'post_status' => 'publish',
        );
        else:
            $args = array(
                'post_type'      => 'faq',
                'posts_per_page' => 6,
                'post_status' => 'publish',
                '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();
            else:
                echo '<div class="c-subtitle is-dark no-found mt-2 mb-4">No content found</div>';
            endif;
    exit;
}

add_action('wp_ajax_listPosts','listPosts');
add_action('wp_ajax_nopriv_listPosts','listPosts');