dezimar
dezimar

Reputation: 45

Wordpress post number

is it possible to show the number of the post (not the ID), like the order in the post list?

For example: I have 30 posts. The newest one has the number 1, the last one has the number 30.

Upvotes: 0

Views: 80

Answers (1)

Carlos E. G. Barbosa
Carlos E. G. Barbosa

Reputation: 51

Do a query to the database, targeting the posts table, selecting post_type = 'post' (or 'page', whichever you want to enumerate), and post_status = 'publish' (otherwise you will enumerate drafts also). If you choose to get an array_N as a result, you may use the array keys to create the "post numbers" you are looking for. This is an example of that query:

global $wpdb; // call the global database object
$html = '';
/* get the title of each post with status 'publish' */
$serial_num_post = $wpdb->get_results( 
    "SELECT post_title 
    FROM $wpdb->posts 
    WHERE post_type = 'post' 
    AND post_status = 'publish'", 
    ARRAY_N );
if ($serial_num_post!=NULL) { // are there any posts?
     foreach ($serial_num_post as $key => $value){
         $html .= 'post# '.($key+1).' - '.$value[0].'<br />';
     }
}
echo 'Total: '.count($serial_num_post).' posts<br />';
echo $html;

Upvotes: 1

Related Questions