WP_Query on custom post type not displaying, multiple loops & get_template_part

I think you forgot to check if $the_query have posts.

<?php

    $args = array(
        'post_type' => 'testimonials',
        'posts_per_page' => 1,
        'orderby' => 'rand'
    );

    $the_query = new WP_Query( $args );

?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <div class="testimonial push_2 grid_10 clearfix">
        <blockquote>&ldquo;<?php the_field( 'testimonial' ); ?>&rdquo;</blockquote>
        <cite>&mdash;<?php the_field( 'name' ); ?></cite>
    </div>

<?php endwhile; else : ?>

    <p>There were no testimonials :( </p>

<?php endif;
wp_reset_postdata(); ?>

And also don’t forget to reset postdata if you are running multiple queries on a page.

Leave a Comment