23 | 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.

This entry was posted in Tips & Tricks, WordPress and tagged , , , . Bookmark the permalink.

32 Responses to Continuous post numbers for your blog

  1. Daniel Barboza comments:

    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?

    • michael replies:

      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 …

  2. sebestial comments:

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

    thanks in advance

  3. Pingback: WordPress: Continuous post numbers for your blog | BheimseinBlog


    28/07/2014 | […] WordPress: Continuous post numbers for your blog […]

  4. kkri comments:

    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

  5. TopTech4u comments:

    Can I follow this process to show the post number in the url? I am rebuilding my site using wordpress and I want to make this
    to look like this
    Keep in mind that I used to do this manually for my past articles, so I dont’ want their url to be changed. Is that possible?
    Thanks a lot

    • Alex replies:

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

  6. cogmios comments:

    this causes on my blog “504 Gateway Time-out” because it takes too much resources.

  7. Víctor comments:

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


    • alchymyth replies:

      as mentioned in the article, you need to save or edit at least one post to trigger the code.

  8. masterwoo comments:

    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.

    • alchymyth replies:

      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.

      • pajura replies:


        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?


  9. Karri comments:

    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!

    • alchymyth replies:

      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 😉

      • Karri replies:


        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!

  10. feliz comments:

    hello. is this possible for tumblr themes too? i want my posts numbered. can you pls help?

    • alchymyth replies:

      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?

  11. Michael comments:

    Would it be possible to exclude certain posts from the numbering system based on their category?

  12. Doug comments:

    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.

    • Doug replies:

      Ok, the_meta returns your_key: per default so have to figure out how to use get_post_meta.

      • Doug replies:

        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>

  13. tom johnson comments:

    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


  14. MK comments:

    Hi is there anyway to reverse so this, so that the *newest* post is called 1?

  15. XL comments:

    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?

    • alchymyth replies:

      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: http://wordpress.org/support/
      see you there…

Leave a Reply

Your email address will not be published.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>