<?php $args = array( 'post_type' => 'posttype', 'posts_per_page' => 20, 'tax_query' => array( array( 'taxonomy' => 'taxonomy', 'field' => 'slug', 'terms' => 'terms', ), ), ); $query = new WP_Query( $args ); $counter = 0; $post_count = $the_query->post_count; ?> <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?> <?php if ($counter == 0) { echo '<div class="row">'; } ?> <!-------------------------------------> <?php $counter++; if ( $counter % 4 == 0 || $post_count == $counter ) { echo '</div> <div class="row">'; } ?> <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?>