[API] Pagination

https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/

Các trang web WordPress có thể có rất nhiều nội dung - nhiều hơn những gì bạn muốn rút xuống trong một yêu cầu. Các điểm cuối API mặc định cung cấp một số lượng hạn chế các mục cho mỗi yêu cầu, giống như cách một trang web WordPress sẽ mặc định là 10 bài đăng trên mỗi trang trong chế độ xem lưu trữ.

Pagination Parameters

Bất kỳ phản hồi API nào chứa nhiều tài nguyên đều hỗ trợ một số tham số truy vấn phổ biến để xử lý phân trang thông qua dữ liệu phản hồi:

?page=: specify the page of results to return

  • ?page=: specify the page of results to return.

    • For example, /wp/v2/posts?page=2 is the second page of posts results

    • By retrieving /wp/v2/posts, then /wp/v2/posts?page=2, and so on, you may access every available post through the API, one page at a time.

  • ?per_page=: specify the number of records to return in one request, specified as an integer from 1 to 100.

    • For example, /wp/v2/posts?per_page=1 will return only the first post in the collection

  • ?offset=: specify an arbitrary offset at which to start retrieving posts

    • For example, /wp/v2/posts?offset=6 will use the default number of posts per page, but start at the 6th post in the collection

    • ?per_page=5&page=4 is equivalent to ?per_page=5&offset=15

Để xác định số lượng trang dữ liệu có sẵn, API trả về hai trường tiêu đề với mọi phản hồi được phân trang:

  • X-WP-Total: tổng số bản ghi trong bộ sưu tập

  • X-WP-TotalPages: tổng số trang bao gồm tất cả các bản ghi có sẵn

Ordering Results

Ngoài các tham số truy vấn phân trang được nêu chi tiết ở trên, một số tham số khác kiểm soát thứ tự của kết quả trả về:

  • ?order=: control whether results are returned in ascending or descending order

    • Valid values are ?order=asc (for ascending order) and ?order=desc (for descending order).

    • All native collections are returned in descending order by default.

  • ?orderby=: control the field by which the collection is sorted

    • The valid values for orderby will vary depending on the queried resource; for the /wp/v2/posts collection, the valid values are “date,” “relevance,” “id,” “include,” “title,” and “slug”

    • See the REST API reference for the values supported by other collections

    • All collections with dated resources default to orderby=date

Last updated