Show post number instead of a date in WordPress

11.Aug.2014

It is not usual to show the number of the published post, however, it is not necessarily a bad thing. Sometimes, we just don’t publish on daily or weekly basis and this “published” date might make our posts look outdated. Therefore, we can build a simple procedure which will tell us how many of this post type has been published before the current article and use it to present our visitors with post number on our blog.

To use procedure shown in snippet below, just call the shortcode post_order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function posts_published_before($post_id, $post_type) {
	global $wpdb;
	return $wpdb->get_var(
			$wpdb->prepare(
				"SELECT count(*)  FROM `$wpdb->posts` "
				. "WHERE `post_status` LIKE 'publish' "
				. "AND `post_type` LIKE '%s' "
				. "AND ID <= %s "
				. " ORDER BY `ID` ASC", $post_type, $post_id
			)
		);
}
 
function post_order_function( $atts ) {
     return "No. " . posts_published_before(get_the_ID(), 'post');
}
add_shortcode('post_order', 'post_order_function');