Skip to main content

On a recent WordPress job I needed a way to count posts and show a number for the post. For example Article 1, Article 2, etc. In my case I need that count to start at 100. So Article 100, Article 101 and so on. I searched for days without ever finding anything, then on a random stumble I found this:

How to display incrementing number next to each published post on WP RECIPES. Apparently, this was also posted on the WP Forum.

This goes in the theme functions:

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 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’ );

This goes in the loop where you want the post numbering to appear:
ID,'incr_number',true); ?>