Use the built in current_post
and post_count
vars with php’s modulus operator to check where you are in the loop and output markup at the appropriate time.
if( $grid_loop->have_posts() ):
echo '<div class="row">';
while( $grid_loop->have_posts() ):
$grid_loop->the_post();
?>
<div class="col-xs-3 box">
<p><?php the_title(); ?></p>
</div>
<?php
// if this is the third post
// and not the last post
// close previous row and open a new one.
// note: current_post starts at zero, so we add 1 to it
if( ((($grid_loop->current_post + 1) % 3) == 0)
&& ($grid_loop->current_post + 1) != $grid_loop->post_count ):
echo '</div><div class="row">';
endif;
endwhile;
echo '</div>';
endif;