Continuous post numbers for your blog

post_numbersif you look at my blog, you may notice that all posts are numbered with a unique number – starting with 1 for the oldest post and increasing continuous for each published post.

how is it done?

with a function that is hooked to some wordpress core actions;

the function takes care of everything:

  • gets a list of all published posts
  • counts through the posts
  • adds or updates the custom field
function updateNumbers() {
/* numbering the published posts: preparation: create an array with the ID in sequence of publication date, /
/ save the number in custom field 'incr_number' of post with ID /
/ 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' );
add_action ( 'deleted_post', 'updateNumbers' );
add_action ( 'edit_post', 'updateNumbers' );

save it to functions.php of your wordpress theme.

anywhere you want to display this post number, use the following line:

<?php echo get_post_meta($post->ID,'incr_number',true); ?>

just be aware that these numbers are not unique numbers ‘for life’ as they will be reallocated when you delete a post, or when you change the post order by changing the publish date.

to trigger the function to generate the custom fields and fill them with the numbers, you need at least to make a new post, edit one post, or delete one post.

PS: the same thing starting with number 1 for the latest post –
different custom field key, hidden.

37 Replies to “Continuous post numbers for your blog”

  1. Hey alchemyth,
    thank you for this code. Is ist possibble to do this for custom posts like
    $wpdb->posts.post_type = ‘my_custom_post’
    Thank you in advance

  2. Hey alchemyth,
    thanks a lot for the continued support here, very much appreciated! I was looking for the exact same thing as Tom Johnson above, which is to exclude posts from a certain category from the displayed count. But somehow the code snippet that you posted taking into consideration the exclusion of certain categories does not work on my end.

    I suppose the root of my problem is that the category i want the posts counted from is a child category of the one i need to be excluded, if that makes sense. I need the category nesting to maintain a logical permalink structure unfortately… To speak in ID’s: I only want posts from category id=2 counted, which is a child of category 5… So inserting 5 in the parameters produces a 0 in post count… In case you have a minute: Do you see any way to get this done in my scenario? Thanks so much in advance…

  3. Hi alchymyth,

    I’m new to php. Could you please tell me in which .php file of my theme i should add the line for incrementing post numbers? Also where should i add it in that file?

    1. where exactly to add the code will depend on your currently used theme.
      some part of the code will go into functions.php ideally of a child theme; the other line for the output goes possibly where your post titles are output, i.e. index.php, archive.php, single.php …

  4. Hello, nice Idea and implementation, could it be used only for a specific category?

    thanks in advance

  5. Hi. That’s what I needed, thanks! Though I’m not a programmer and I want to do some more. I’d like to include 2 categories of custom post types and assign 2 different numbers sets, also choose a number to begin with (not from 1 but 1000) and display the number above or below the post. May all these choices be joined in a plugin as parameters and allowing us to download from the WordPress repository? It would be great! Thank you

    1. Hello ,
      Install plugin “Custom Fields Permalink” and in custom url structure add “%cfp_incr_number%” hope that helps!

  6. Hello,
    Can not work with the latest version of wordpress?
    I follow the instructions but I do not see any results.


  7. I want to give each post a unique number or ID and show is along with the title. I’ve a website on customer complaints and each post is a unique complaint.

    Is there a way to add a unique number which would be a complaint number in my case and will not change even if post is edited, deleted and so on.

    Basically a unique number associated with each post title which is always unique and does not change forever.

    Thanks in advance.

    1. the easisest way is to use the post ID as it is already unique – although it could look odd as it does not increment in single steps.

      1. Hi,

        tried the code and works fine.

        Like the person above I want to use a unique number for the post that is incremental and is not post ID.
        Is there a fix to make that?


  8. Hi alchymyth,

    Thanks for the guide.

    I’m writing 365 posts in WordPress, everyday, for one year. I would like to add next to my title, which day is currently. For example, I if would to start today, this day would be Day 1. Tomorrow would be Day 2, the day after that Day 3, and so on.

    I tried to add your code (which from my understanding would work just fine in my case) but when I add your code to my functions.php file, WordPress throws an error, saying it conflicts with some pluggables.php file in my wp-includes folder. I have no idea what’s going on.

    The php yday function does not work for me either, as it’s counting from the beginning of the year.

    Any thoughts?

    Thanks in advance!

    1. Hi Karri,

      using exactly the php date functions, the code below does work, even if it might not be the most elegant solution:
      <?php echo (get_the_time('z')+1000*get_the_time('Y')) - (date('z',mktime(0, 0, 0, 8, 21, 2011)) + 1000*date('Y',mktime(0, 0, 0, 8, 21, 2011))); ?>

      8, 21, 2011 is the reference time in month, day, year format.

      good luck 😉

      1. Hi,

        it’s me again. Actually, I got your original code working just fine. Turned out that I forgot to add <?php in the beginning in my functions file. After adding that, the code works fine, at least from what I can tell. A test post for this is live on my site.

        I can test your other code as well and see how it works.

        Thanks again alchymyth!

    1. i would not know. on a first glance, it looks rather unlikely with the basic tumblr functions.
      does tumblr have a support forum? have you asked there?

  9. Nice work!

    I’m trying to alter the Recent Posts widget in a default install of WP 3.01, to show and link using this post number.
    In default-widgets.php, using:

    <li ><a href="<?php the_permalink();?>" ><?php the_meta($post->ID,'incr_number',true);?></a></li>

    I get “incr_number: 1” . Not sure where the “incr_number: ” part comes from and how to get rid of it. Also lack the php skills to understand why “the_meta” works and not “get_post_meta”. Tried returning “get_post_meta” to a variable and echo out but no luck.

    Any advice? Tthanks.

      1. And replacing $post-ID with get_the_ID() did the trick!

        <li><a href="<?php the_permalink();?>" ><?php echo get_post_meta(get_the_ID(),'incr_number',true);?></a></li>

  10. This is really useful – thanks for writing it!

    I was also wondering if it is possible to amend it so that the number is always at least 2 digits
    so it will start 01, 02, 03 etc?
    Then when it gets to 10 it will just be 10, 11, 12 etc


    1. hi tom,
      nice idea for the formatting of the numbers.
      i checked the php tutorial, and found that this should work:
      <?php printf("%02d", get_post_meta($post->ID,'incr_number',true));?>

      1. Thanks very much for your prompt response – it’s much appreciated and very kind of you!

  11. is there a simple version of this?

    for example: on the blog roll, I would like each post to be numbered 1-10. When you go to the next page, the posts are again numbered 1-10.

    How can that be done?

    1. hi XL,
      simply use a counter variable, which is set to 1 before the loop, and echoed with the post title, then incremented inside the loop.
      if you need more help, consider joining the wordpress support forum:
      see you there…

Leave a Reply

Your email address will not be published. Required fields are marked *