WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am wanting to display the number order of a post. I tried using<?php the_ID(); ?> but this just displays the post ID and doesn't seem to do go in sequential order due to post revisions and whatnot.

Is there a way to to display if the post is #2 or #3 or #4 and so on?


share|improve this question
up vote 6 down vote accepted

Post ID's aren't meant to be sequential. To number your posts sequentially, you'd have to use a meta field. There was discussion of this while back on this question:

Change Permalinks Structure to a Sequential Number for Each Post?

And the best answer for what you're looking to do seemed to be a snippet of code posted on the support forums:


function updateNumbers() {
    /* numbering the published posts, starting with 1 for oldest;
    / creates and updates custom field 'incr_number';
    / to show in post (within the loop) use <?php echo get_post_meta($post->ID,'incr_number',true); ?>
    / alchymyth 2010 */
    global $wpdb;
    $querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts 
                 WHERE $wpdb->posts.post_status = 'publish' 
                 AND $wpdb->posts.post_type = 'post' 
                 ORDER BY $wpdb->posts.post_date ASC";
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    $counts = 0 ;
    if ($pageposts):
    foreach ($pageposts as $post):
        add_post_meta($post->ID, 'incr_number', $counts, true);
        update_post_meta($post->ID, 'incr_number', $counts);

add_action ( 'publish_post', 'updateNumbers', 11 );
add_action ( 'deleted_post', 'updateNumbers' );
add_action ( 'edit_post', 'updateNumbers' );

(I modified it very slightly to sort by post date, rather than just ID as the initial code did.)

It will hook into all post actions (save/publish/delete) and update the 'incr_number' custom field for all posts. So you'll have a performance lag on saving posts when you have a large number in the database, but once the posts are saved, showing the number is as simple as

echo get_post_meta( $post->ID, 'incr_number', true );
share|improve this answer
You are awesome. Thank you! – Red Mar 26 '11 at 21:44
I have a question wrt this: (quote) "The numbers will be updated any time a post is - deleted, edited, or published. For instance, if you delete a post or set it to draft, the maximum number will shrink by 1, and the remaining posts will be renumbered. Therefore, this is not a post number for life, but reflects the numbers of all published posts." — Does removing add_action ( 'deleted_post', 'updateNumbers' ); from the code solve the renumbering problem? (I want permanent numbers for all posts, unaffected once set) – its_me Jul 28 '12 at 3:18
If you want your post numbers to remain constant for life, why not just use the post ID's? That seems to be what they're for... – goldenapples Jul 31 '12 at 16:39
Worked a treat - thanks. – James Kemp Oct 13 '13 at 10:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.