How to check if a WP_Query has data

Change it up a bit and use have_posts method to check if there are any results:

<?php 
$args = array(
    'post_type' => 'questions',
    'posts_per_page' => '3',                                        
    'tax_query' => array(
        array(
        'taxonomy' => 'types',
        'field' => 'slug',
        'terms' => 'customer-service'
        )
    )
);

$loop = new WP_Query( $args );
if ($loop->have_posts()){
?>
<h4>Frequently Asked Questions</h4>

<ul class="faq">
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <li><a href="https://wordpress.stackexchange.com/questions/112118/<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
</ul>
<?php }

Leave a Comment