Maarten Wolfsen Maarten Wolfsen - 7 months ago 21
PHP Question

Get the current post number (not ID) in wordpress

I am using this function to get the amount of posts:

$postAmount = wp_count_posts( 'post' )->publish;


This returns 4, which is the right number.
But, is there also a function, where I can check the current post number? Not the ID, but only a number.

For example, I am at the second post, so I want the function to return '2'.

Extra information

$wp->query->current_post+1
returns 0 at every post

Answer

You've got the right idea, but just made a small typo. What you want is

$wp_query->current_post

or

$wp_query->current_post + 1

depending on wether or not you want to count starting at zero.

enter image description here

Search for current_post in the WP_Query Codex https://codex.wordpress.org/Class_Reference/WP_Query

Comments