-
Notifications
You must be signed in to change notification settings - Fork 83
8. REST API Endpoints
With the release of version 4.7, WordPress -finally- introduced its very own REST API:
Content endpoints provide machine-readable external access to your WordPress site with a clear, standards-driven interface, paving the way for new and innovative methods of interacting with sites through plugins, themes, apps, and beyond.
WordPress Popular Posts (4.1+) provides its own REST API endpoints so you can display your popular entries on your (web) application.
Get links to all other endpoints available in the API.
- HTTP method: GET
-
URLs:
/wp-json/wordpress-popular-posts/v1/
and/wp-json/wordpress-popular-posts/v2/
Get popular posts.
- HTTP method: GET
-
URL:
/wp-json/wordpress-popular-posts/v1/popular-posts/
Return popular posts from specified (custom) post types (separated by comma).
- Required: no
- Default: "post"
- Type: string
Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?post_type=post,page
The maximum number of popular posts to return.
- Required: no
- Default: 10
- Type: integer
Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?limit=5
Retrieve the most popular entries published within the specified time range.
- Required: no
- Default: "0" (0 = disabled, 1 = enabled)
- Type: string
Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?freshness=1
An offset point for the collection.
- Required: no
- default: 0
- Type: integer
Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?offset=5
Set the sorting option of the popular posts.
- Required: no
- Default: "views" (possible values: "views", "comments")
- Type: string
Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?order_by=comments
Return posts from a specified time range.
When using the value "custom" in conjunction with the parameters time_unit
and time_value
(see below) you can retrieve popular posts from a custom defined time range (eg. last 12 hours).
- Required: no
- Default: "last24hours" (possible values: "last24hours", "last7days", "last30days", "all", "custom")
- Type: string
Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?range=last7days
Specifies the time unit of the custom time range.
- Required: false
- Default: "hour" (possible values: "minute", "hour", "day", "week", "month")
- Type: string
Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?range=custom&time_unit=hour&time_quantity=12
Specifies the number of time units of the custom time range.
- Required: no
- Default: 24
- Type: integer
Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?range=custom&time_unit=hour&time_quantity=12
Post IDs to exclude from the listing (comma separated).
- Required: no
- Type: string
Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?pid=56,75,109
Include posts in a specified taxonomy.
To filter posts by taxonomy you also need to specify the term ID(s) via term_id
.
- Required: no
- Default: "category"
- Type: string
Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?taxonomy=post_tag&term_id=115
Taxonomy IDs, separated by comma (prefix a minus sign to exclude).
- Required: no
- Type: string
Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?taxonomy=post_tag&term_id=115
Include popular posts from author ID(s).
- Required: no
- Type: string
Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?author=1,3,7
Returns the views count of a given post / page / custom post type ID.
- HTTP method: GET
-
URL:
/wp-json/wordpress-popular-posts/v2/views/[post_id]
The time range.
- Required: false
- Default: "all" (possible values: "last24hours", "last7days", "last30days", "all", "custom")
- Type: string
Usage: /wp-json/wordpress-popular-posts/v2/views/[post_id]?range=last24hours
Specifies the time unit of the custom time range.
- Required: false
- Default: "hour" (possible values: "minute", "hour", "day", "week", "month")
- Type: string
Usage: /wp-json/wordpress-popular-posts/v2/views/[post_id]?range=custom&time_unit=hour&time_quantity=12
Specifies the number of time units of the custom time range.
- Required: no
- Default: 24
- Type: integer
Usage: /wp-json/wordpress-popular-posts/v2/views/[post_id]?range=custom&time_unit=hour&time_quantity=12
Whether to include the "views" text as part of the response (eg. 2,932 views).
- Required: no
- Default: 1
- Type: integer
Usage: /wp-json/wordpress-popular-posts/v2/views/[post_id]?include_views_text=0
Updates the views count of a given post / page / custom post type ID.
- HTTP method: POST
-
URL:
/wp-json/wordpress-popular-posts/v2/views/[post_id]
Whether to apply Data Sampling to this page view request. Default is disabled (0).
- Required: false
- Default: 0 (possible values: 0, 1)
- Type: integer
The sampling rate.
- Required: false
- Default: 100
- Type: integer
Usage: /wp-json/wordpress-popular-posts/v2/views/[post_id]
Note that to send a POST request to this endpoint you must provide a valid nonce called '_wpnonce'
either via the _wpnonce
parameter or the X-WP-Nonce
request header, otherwise the REST API will return an HTTP 401 Forbidden response.