title: Calculate Position/Rank for returned post
body: <p>Here is the custom post declaration</p>
  
  <pre><code>function ts_result_reg() {
  $labels = array(
  'name' =&gt; _x( 'Students Result', 'post type general name' ),
  'singular_name' =&gt; _x( 'Students Result', 'post type singular name' ),
  'add_new' =&gt; _x( 'Add New Result', 'result' ),
  'add_new_item' =&gt; __( 'Add New Result' ),
  'edit_item' =&gt; __( 'Edit Result' ),
  'new_item' =&gt; __( 'New Result' ),
  'all_items' =&gt; __( 'All Results' ),
  'view_item' =&gt; __( 'View Result' ),
  'search_items' =&gt; __( 'Search Results' ),
  'not_found' =&gt; __( 'No result found' ),
  'not_found_in_trash' =&gt; __( 'No result found in the Trash' ),
  'parent_item_colon' =&gt; '',
  'menu_name' =&gt; 'Student Results'
   );
   $args = array(
   'labels' =&gt; $labels,
   'description' =&gt; 'Add new custom post type students result',
   'public' =&gt; true,
   'menu_position' =&gt; 5,
   'supports' =&gt; array( 'thumbnail','title', ),
   'taxonomies' =&gt; array( 'classes' ),
   'has_archive' =&gt; true,
   'rewrite'=&gt; array ('slug' =&gt; 'tscheck'),
   );
   register_post_type( 'ts_students_result', $args );
   }
   add_action( 'init', 'ts_result_reg' );
  </code></pre>
  
  <p>The backend metaboxes</p>
  
  <pre><code>array(
              'name' =&gt; 'French',
              'id'   =&gt; $prefix . 'subject_french_exam_total',
              'type' =&gt; 'text_small',
              // 'repeatable' =&gt; true,
          ),
  
  array(
              'name' =&gt; 'Business Education,
              'id'   =&gt; $prefix . 'subject_business_edu_exam_total',
              'type' =&gt; 'text_small',
              // 'repeatable' =&gt; true,
          ),
  
          ...
  </code></pre>
  
  <p>The front end query</p>
  
  <pre><code> query_posts( array(
  ...
  )
      ),
  'posts_per_page' =&gt; 100
     )
  );
  ?&gt;
  &lt;table style="border:1px solid black" cellpadding="1.5" cellspacing="5"&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;th&gt;&lt;strong&gt;NAME\SUBJECT&lt;/strong&gt;&lt;/th&gt;
          &lt;th align='center'&gt;&lt;strong&gt;French&lt;/strong&gt;&lt;/th&gt;
          &lt;th align='center'&gt;&lt;strong&gt;Business Education&lt;/strong&gt;&lt;/th&gt;
          &lt;th align='center'&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/th&gt;
          &lt;th align='center'&gt;&lt;strong&gt;Average&lt;/strong&gt;&lt;/th&gt;
          &lt;th align='center'&gt;&lt;strong&gt;Position&lt;/strong&gt;&lt;/th&gt;
      &lt;/tr&gt;
      &lt;?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?&gt;
      &lt;!-- Initializing count and sum --&gt;
      &lt;?php
          $subject_count=0;
          $subject_sum_total=0;
  
           $subject_french_exam_total= get_post_meta(get_the_ID(),'_ts_subject_french_exam_total',true );
           if($subject_french_exam_total):
           $subject_count++;
           $subject_sum_total+=$subject_french_exam_total;
           endif;
  
           $subject_business_edu_exam_total= get_post_meta(get_the_ID(),'_ts_subject_business_edu_exam_total',true );
           if($subject_business_edu_exam_total):
           $subject_count++;
           $subject_sum_total+=$subject_business_edu_exam_total;
           endif;
  
          $store_student_avg=array();
        if($subject_sum_total&gt;0.1):
        $subject_avg_total= $subject_sum_total/$subject_count;
        array_push($store_student_avg,$subject_avg_total);
         endif;
      ?&gt; 
      &lt;tr&gt;
      &lt;td&gt;&lt;?php the_title(); ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;?php echo $subject_french_exam_total ;?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;?php echo $subject_business_edu_exam_total ;?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;?php echo $subject_sum_total ;?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;?php echo $subject_avg_total ;?&gt;&lt;/td&gt;
  
  &lt;?php 
  rsort($store_student_avg);
  $arrlength = count($store_student_avg);
  $rank = 1;
  $prev_rank = $rank;
  for($x = 0; $x &lt; $arrlength; $x++)
   if ($x==0): ?&gt;
      &lt;td&gt;&lt;?php echo $rank; ?&gt;&lt;/td&gt;
  
     &lt;?php elseif ($numbers[$x] != $numbers[$x-1]):
      $rank++;
      $prev_rank = $rank;
      ?&gt;
      &lt;td&gt;&lt;?php echo $rank; ?&gt;&lt;/td&gt;
  
     &lt;?php else:
      $rank++;
      ?&gt;
         &lt;td&gt;&lt;?php echo $prev_rank; ?&gt;&lt;/td&gt;
  
  &lt;?php endif; ?&gt;
  
      &lt;/tr&gt;
      &lt;?php endwhile;endif; ?&gt;   
  &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Edited: In the above code(shortened) I need the positions(rank) for each post's average calculated. Seems the whole loop is not run in wordpress before the output, hence, the output for each post is 1 (<a href="https://i.stack.imgur.com/tPNXH.png" rel="nofollow noreferrer">screenshot of output</a>). How do I solve this?</p>