A fresh mix of code snippets and tutorials

Display a number for each post

To display an auto-incrementing number for each post, add the following snippet to your theme’s functions.php file:

function updateNumbers() {
	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');

Then display the number for each post by including this tag anywhere in the WordPress loop:

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

You can see this technique in action here at WP-Mix, just visit the home page and look at the numbered footer for each post.

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress