Skip to content

8. REST API Endpoints

Héctor Cabrera edited this page Aug 24, 2018 · 1 revision

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
  • URL: /wp-json/wordpress-popular-posts/v1/

/popular-posts

Get popular posts.

  • HTTP method: GET
  • URL: /wp-json/wordpress-popular-posts/v1/popular-posts/

Parameters

post_type

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

limit

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

freshness

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

offset

An offset point for the collection.

  • Required: no
  • default: 0
  • Type: integer

Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?offset=5

order_by

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

range

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

time_unit

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

time_quantity

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

pid

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

taxonomy

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

term_id

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

author

Include popular posts from author ID(s).

  • Required: no
  • Type: string

Usage: /wp-json/wordpress-popular-posts/v1/popular-posts?author=1,3,7

/popular-posts/widget

Get the HTML output of a given WPP widget instance.

  • HTTP method: GET
  • URL: /wp-json/wordpress-popular-posts/v1/popular-posts/widget

Parameters

id

The id of the widget instance.

  • Required: yes
  • Type: integer

Usage: /wp-json/wordpress-popular-posts/v1/popular-posts/widget?id=8

You can’t perform that action at this time.