Support » Fixing WordPress » Display sequential post number/count (not post id)??

  • Resolved rickyaustin


    I’m creating a small inspiration site, and I would like to number each post in chronological order.

    I thought this would be a piece of cake, but I’m not a PHP ninja, and while I’ve made a few themes, I just haven’t run across this functionality, so it may be easy, maybe not. I’ve seen it done pretty simply on numerous sites for comments. Just assumed it’d be simple for posts. Three hours worth of searching has turned up absolutely nothing.

    Since the post ID’s jump around and aren’t perfectly numbered, I can’t use that for this.

    I want the oldest published post to show a 1, second to show a 2, and so on, globally across the entire site, with the most recent showing the highest # — not resetting on each page full of posts — all the way up to the thousands I’ll end up having. This does not have to work on single.php, only really need it when showing X number of posts in the loop on index and subsequent pages.

    So if I go to index and have 150 posts published on the total site, with 50 posts per page, I should see a # 150, 149, 148…101, Page 2 should show 100, 99…51, Pg 3 50,49…1

    My initial guess was to begin playing with wp_count_posts or total_posts and count in some way, but I just don’t have the skills to dream it up from scratch. Could be completely wrong too 😀

    It’s just a minor site detail, I can live without it, but wanted to ask the question before giving up or doing it manually. Would really love this to be automated. Thanks.

Viewing 9 replies - 1 through 9 (of 9 total)
  • i use this in functions.php of the theme:

    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' ";
    $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' );

    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.

    to show it (in any template file, but has to be in the loop) use:
    <?php echo get_post_meta($post->ID,'incr_number',true); ?>

    Thanks for this, however I dropped this into two themes I’ve made, and two wordpress default themes to test it, and all didn’t work. No errors thrown, just nothing showed up in the html.

    I found your site, and saw that it really is working there, so I know it’s possible! Also tried copy/pasting the code from your post about this, incase there was an error in reposting here, same result.

    Is there something else to this that you may have inadvertently overlooked to post as part of this?

    Your description is exactly the functionality I’m searching for. Thanks

    did you check the custom fields under the posts in ‘post edit’ to see if the fields are created and filled properly?

    also did you add this
    <?php echo get_post_meta($post->ID,'incr_number',true); ?>
    into index.php or single.php where you want to see the numbers?

    edit: (forget the above – it has to do with the version of wp)

    tested it with wp3, and you are right – nothing showing; the custom fields are not getting generated.

    i am going to check what is different in wp3 and report back here (and in my blog)

    (short progress:
    in my test blog on wp3, after i manually added the custom field to one of the posts, it started working and spread over all existing posts, and seems to be numbering ok.)

    final thought:
    you would need to at least make a new post, or edit or delete one post to trigger the function to create the custom field and fill it with the numbers.


    You’re the man. Needed to make the new post to trigger it.

    Many many thanks. It’s working as intended.

    Hi, I’ve also installed your script. It works great. Only problem is that the code in the index.php is published the other way round. In the html the top id=9 and the bottom =1. It needs to be the other way around for me. So that way I can use moving boxes:

    Can you help me with that?


    do you need to have #panel_1 with t' koppeltje and #panel_9 with femminile ?

    or do you need to have femminile on the left and t' koppeltje on the right?

    (it is standard behaviour for the loop to show the latest post first; you can change this by adding a query_posts() before the loop)

    in index.php (or the template file that shows the front page), add something like this before the line with while(have_posts()) :

    <?php query_posts($query_string . '&orderby=date&order=ASC'); ?>

    Tnx for your quick reply, the query_post did the trick!! Almost too easy…

    Do you know if it’s still possible to alter the ‘post order’ like I normally do with ‘postmash’. Your code overrules postmash and it would be great if I can alter the ‘post order’ whenever I want.

    Does ‘orderby=menu_order’ do the trick?

    This worked perfectly for me, thanks a lot alchymyth!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Display sequential post number/count (not post id)??’ is closed to new replies.