How do I find the count of the current post?

Let's say my custom post type is 'story'. I've got 20 stories published on my site. So each post becomes: 'story 1', 'story 2', ... 'story 20'. Thus, first post is 'story 1', and last post is 'story 20'. Now when I open one of these custom posts, I want to see the number. Example, if I open the ninth (published) post I want to see it mentions 'story 9'. I don't want to hardcode it which I can do using ACF plugin. I want that data to be generated dynamically, just like how wordpress dynamically generates post id or slug.

Topic count posts custom-post-types Wordpress

Category Web


Following @mozboz answer, I wrote an "explicit version" of the code (the previous throw me an error). This worked for me, in my single.php footer:

$count = 0;
$post_array = Array();

$args = Array(
    'post_type'     => 'post',
    'orderby'       => 'date',
    'order'         => 'DESC',
    'posts_per_page'=> -1
);
$posts_query = new WP_Query( $args );

if ( $posts_query->have_posts() ) {
    while ( $posts_query->have_posts() ) {
        $posts_query->the_post();
        $post_id = get_the_ID();
        $post_array[] = $post_id;
        $count++;
    }
} 
wp_reset_postdata();

$post_id = get_the_ID();
$position = array_search($post_id, $post_array);

echo $position +1 . '/' . $count;

There isn't an order or numbering of posts like this that is stored in Wordpress.

The database stores things with a 'primary key', which is just a number, but this always stays the same for each post, forever. So if you add lots of posts and remove lots of posts then add some more, the Primary Key for the first 3 posts might be 1,20,100.

If you want to find the 'count' or the 'order' of posts like in your question then you have to choose how you want to sort them and then find the order yourself.

For example, you could use WP_Query to get all your posts ordered by date, and then print them in order giving them numbers 1,2,3,4, and you could also use this list to determine what number in this list any post was.

HTH. Please expand your question if you need more information.

EDIT:

So according to your updated question you need to do something like this. You could store this number in a post_meta field and hook into the save post hook to update this if you wanted to - that would be more efficient than running this on every page load.

$args = Array(
    'orderby' => 'date',
    'order' => 'ASC' // oldest first
}

$postsSorted = get_posts($args);

$count = 1;
$postToNumbers = Array();


foreach($postsSorted as $post) {
    $postNumbers[$post->ID] = $count;
    $count++;
}

This would give you an array that lets you look up the 'count' for any post ID. So you use this like:

$postID = get_the_ID();  // whatever method is relevant to where you are to get the post ID 
echo "The count of this post is " . $postNumbers[$postID] ;

You could put this in a function to make it easy to call, or as above you could hook it into the save / delete posts hooks to store the number in post meta and make the number get updated whenever posts are added removed, but you'll need to write the code for that ;-)

Does that help?

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.