Skip to content

Instantly share code, notes, and snippets.

@NeilJS
Created March 20, 2012 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeilJS/2137806 to your computer and use it in GitHub Desktop.
Save NeilJS/2137806 to your computer and use it in GitHub Desktop.
WP - Get current page number for current (single) post inc. CPTs
/* GET CURRENT PAGE NUMBER FOR THIS SINGLE POST ie. it's index number */
// put in functions.php
// Note: edit "my-custom-post-type" or remove "AND post_type = 'my-custom-post-type'"
function current_post_num($display=true) {
global $post, $wpdb;
//$rows = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date ASC");
$SQL = $wpdb->get_col("SELECT ID FROM $wpdb->posts posts WHERE posts.post_status = 'publish' AND post_type = 'my-custom-post-type' ORDER BY post_date ASC");
$SQL = array_flip($SQL);
$postnum = $SQL[$post->ID];
$postnum++;
if($display) {
echo $postnum;
}
return $postnum;
}
// Usage
<?php current_post_num(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment