The global $wp_query
(in this case its the $query
var) holds the $current_post
which is available in the loop and tells the index of the current post ( $wp_query->current_post
). The $wp_query->post_count
tells the number of posts being displayed in the loop, and this actually becomes a simple arithmetic solution that will work no matter how many posts the loop shows:
if (have_posts()) : while ($query->have_posts()) : $query->the_post();
// code
$counter = $query->post_count - $query->current_post + 1;
// code
endwhile; endif;
Give it a try and please feedback this if it works for you.