Get the Current Page Number

When WordPress is using pagination like this, there’s a query variable $paged that it keys on. So page 1 is $paged=1 and page 15 is $paged=15.

You can get the value of this variable with the following code:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

Getting the total number of pages is a bit trickier. First you have to count all the posts in the database. Then filter by which posts are published (versus which are drafts, scheduled, trash, etc.). Then you have to divide this count by the number of posts you expect to appear on each page:

$total_post_count = wp_count_posts();
$published_post_count = $total_post_count->publish;
$total_pages = ceil( $published_post_count / $posts_per_page );

I haven’t tested this yet, but you might need to fetch $posts_per_page the same way you fetched $paged (using get_query_var()).

Leave a Comment