Title: Ger posts sorting order number
Last modified: August 20, 2016

---

# Ger posts sorting order number

 *  [destrones](https://wordpress.org/support/users/destrones/)
 * (@destrones)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/)
 * Hi,
 * im sorting my posts in a certain way (by rating) what i need to do is to fetch
   the number they have according their position on the list. For example, the first
   post should be 1, the second 2 and so on. I wonder if its possible and how.
 * Thank you.

Viewing 14 replies - 1 through 14 (of 14 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899149)
 * I believe that you just need to add a counter in your Loop and increment it for
   every post.
 *  Thread Starter [destrones](https://wordpress.org/support/users/destrones/)
 * (@destrones)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899191)
 * Thats a good idea. I would appreciate if you have a suggestion on how to do it.
 * Thank you.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899192)
 * The exact instructions depend on your theme and the template that is displaying
   the posts. In general, your code for incrementing the variable might look something
   like this:
 *     ```
       if ( have_posts() ) : while ( have_posts() ) : the_post();
          ++$post_counter;
          // more of the loop code here
          echo "<p>Counter: $post_counter</p>";
          // even more of the loop code
       endwhile; endif;
       ```
   
 * Of course, you would need to echo or otherwise display the $post_counter where
   you wanted it, not as I have shown above.
 *  Thread Starter [destrones](https://wordpress.org/support/users/destrones/)
 * (@destrones)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899193)
 * Nevermind, i used <?php $i = 1; while (have_posts()) : the_post(); ?> and then
   <?php echo $i++; ?> which worked. However, it only looks fine on the first page
   since the counter shows the order of the posts FOR the current page and not overall.
   That means all the pages have a post number 1, 2, 3 and so on so i think the 
   counter on the loop is not gonna work. Any suggestions?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899194)
 * You can set the initial value of the counter using the posts per page and the
   current page number. Assume the posts_per_page is in $posts_per_page and the 
   current page number is in $paged. The formula for the initial value is:
 *     ```
       $i = ($paged - 1) * $posts_per_page +1;
       ```
   
 *  Thread Starter [destrones](https://wordpress.org/support/users/destrones/)
 * (@destrones)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899195)
 * That looks good. Is $paged and $posts_per_page are already default WP variables?
   or do i still need to define them myself?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899196)
 * Both ‘should be’ global variables, but $paged might be zero, so you have to check
   for that.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899197)
 * Here is a modified formula that should work:
 *     ```
       $i = ( max($paged,1) - 1 ) * $posts_per_page + 1;
       ```
   
 *  Thread Starter [destrones](https://wordpress.org/support/users/destrones/)
 * (@destrones)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899198)
 * Yes, $paged is zero. Wehre should i put that formula? before the loop starts 
   or inside the loop?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899199)
 * Where you had $i = 1 before.
 *  Thread Starter [destrones](https://wordpress.org/support/users/destrones/)
 * (@destrones)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899200)
 * Right now i have it like this
 * <?php $i=1; while (have_posts()) : the_post(); ?>
 * <?php echo $i++; ?>
 * so if i use your formula it should be like this?
 * <?php $i = ( max($paged,1) – 1 ) * $posts_per_page + 1; while (have_posts()) :
   the_post(); ?>
 * <?php echo $i++; ?>
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899201)
 * Yes.
 *  Thread Starter [destrones](https://wordpress.org/support/users/destrones/)
 * (@destrones)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899202)
 * I tried but didnt work. However this did the trick for the page 2 and on:
 * <?php $i=1; while (have_posts()) : the_post(); ?>
    <?php $rank=$i++; echo $rank
   + (($paged – 1) * $posts_per_page); ?>
 * but since $paged is 0 when on the first page, it doesnt work for page 1. But 
   i think we are close.
 *  Thread Starter [destrones](https://wordpress.org/support/users/destrones/)
 * (@destrones)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899204)
 * Nevermind again. I had fucked up. Your code actually works, thank you!

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Ger posts sorting order number’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 14 replies
 * 2 participants
 * Last reply from: [destrones](https://wordpress.org/support/users/destrones/)
 * Last activity: [13 years, 11 months ago](https://wordpress.org/support/topic/ger-posts-sorting-order-number/#post-2899204)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
