skip to Main Content

I have an API call from Vue, that makes paginating of WordPress posts, the number of pages and offset work perfectly, I can make first page, previous and next page queries work perfectly with the offset parameter

https://example-page.com/wp-json/wp/v2/posts?categories=5&per_page=12&offset=12

But, for the last page I don’t know what should be the offset, if the number of posts change. How can I find out this number, which is the last page in the list?

2

Answers


  1. You can get it in the header response with this code:

    X-WP-Total: total items
    X-WP-TotalPages: total pages/last page number
    
    Login or Signup to reply.
  2. You can retrieve the total number of pages from the X-WP-Total response header.

    From that you can compute an offset for the last page.

    const itemsPerPage = 12;
    const xWPTotal = /* value from response header */
    const offset = itemsPerPage * Math.round(xWPTotal / itemsPerPage - 1)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search