if 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):
setup_postdata($post);
$counts++;
add_post_meta($post->ID, 'incr_number', $counts, true);
update_post_meta($post->ID, 'incr_number', $counts);
endforeach;
endif;
}
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.
Remark:
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.

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
http://mysite.com/2013/04/08/sample-post/
to look like this
http://mysite.com/2013/04/08/post_number-sample-post/
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
this causes on my blog “504 Gateway Time-out” because it takes too much resources.
Hello,
Can not work with the latest version of wordpress?
I follow the instructions but I do not see any results.
Greetings!
as mentioned in the article, you need to save or edit at least one post to trigger the code.
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.
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.
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?
Thanks!
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!
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
Hi,
it’s me again. Actually, I got your original code working just fine. Turned out that I forgot to add
<?phpin 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!
hello. is this possible for tumblr themes too? i want my posts numbered. can you pls help?
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?
Would it be possible to exclude certain posts from the numbering system based on their category?
yes,
by changing the query at the start of the code; example http://wordpress.pastebin.com/88Cigg7g
only downside of this code is, that it will not remove numbers if you change the ids of the excluded categories.
hope that helps.
Brilliant – thanks so much.
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.
Ok, the_meta returns your_key: per default so have to figure out how to use get_post_meta.
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>
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
Thanks
Tom
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));?>Works perfectly
thanks for the swift reply!
Hi is there anyway to reverse so this, so that the *newest* post is called 1?
This is possible –
see http://wordpress.pastebin.com/WnDfEHWu
the changes are:
$counts = count($pageposts)+1 ;
and:
$counts–;
I changed the name of the function and the custom field key to be able to test it.
Thanks very much for your prompt response – it’s much appreciated and very kind of you!
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?
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…