Get the number of posts from the current page results

I am trying to get the current number of posts that will be present in a page.

$wp_query->found_posts will give me the total number of posts in the loop.

get_query_var( 'posts_per_page' ) will tell me the number of posts per page.

get_query_var( 'paged' ) will give me the current page number.

sticky posts seem to not be included in the above.

I can do some maths trying to figure out in what page I am, retrieve the number of posts, and consider the sticky. I could also add a counter in the loop, but I am trying to get the number before the loop is applied in the template (front-end).

Is there some variable in the $wp_query that I am missing for this goal?


To make it clear this is an example: Total posts from the loop 22. Posts per page 10. 1 sticky post.

  • Page 1 should give 11 (10 + 1 sticky).
  • Page 2 should give 10.
  • Page 3 should give 2.

1 Answer
1

I found the solution. I was looking for:

$wp_query->post_count

And some of my assumptions were not correct. This is the actual output:

Total posts from the loop 22. Posts per page 10. 1 sticky post.

  • Page 1 gives 10 (9 + 1 sticky).
  • Page 2 gives 10.
  • Page 3 gives 2.

Leave a Comment