Search code examples
wordpresswordpress-rest-api

To get different response for single post and multiple posts from WP REST API


Is there any option to get different type response for WordPress single Post and Posts list? My target for posts list response as

[
{"id":1,
"date":"2017-08-20T07:26:55",
"link":"http://localhost/wordpress/2017/08/20/test-post",
"title":{"rendered":"Test post"}
},
{"id":2,
"date":"2017-08-20T07:26:55",
"link":"http://localhost/wordpress/2017/08/20/test-post",
"title":{"rendered":"Test post"}
},
{"id":3,
"date":"2017-08-20T07:26:55",
"link":"http://localhost/wordpress/2017/08/20/test-post",
"title":{"rendered":"Test post"}
},
{"id":4,
"date":"2017-08-20T07:26:55",
"link":"http://localhost/wordpress/2017/08/20/test-post",
"title":{"rendered":"Test post"}
}
]

For single post response as

{
  "id": 92,
  "date": "2017-08-20T07:13:42",
  "date_gmt": "2017-08-20T07:13:42",
  "guid": {
    "rendered": "http://devel8/wp-news/?p=1"
  },
  "modified": "2017-08-20T07:13:42",
  "modified_gmt": "2017-08-20T07:13:42",
  "slug": "hello-world-2",
  "status": "publish",
  "type": "post",
  "link": "http://localhost/wordpress/2017/08/20/hello-world-2/",
  "title": {
    "rendered": "Hello world!"
  },
  "content": {
    "rendered": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n",
    "protected": false
  },
  "excerpt": {
    "rendered": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n",
    "protected": false
  },
  "author": 1,
  "featured_media": 0,
  "comment_status": "open",
  "ping_status": "open",
  "sticky": false,
  "template": "",
  "format": "standard",
  "meta": [],
  "categories": [
    1
  ],
  "tags": [],
  .....
  .....
  }
}

Note: using register_rest_field() and rest_prepare_post filter we can modify the response for both (single and multiple posts) But we need separately response.

OR

There is any option to know the request is made for multiple posts or single post in the get_callback function of register_rest_field().

Thanks in advance.


Solution

  • Since i don't get any response and solution from any one, so i had decided to develop a WordPress plugin which will meet my requirement to handle WP REST API request response for single post and posts list or multiple posts or loop of post or group of posts differently and admin can control from back end.

    After long struggle i have developed the plugin named as

    One Call – WP REST API Extension

    Core features of plugin are

    • Custom and back end control rest api prefix such as ‘test-api’ where ‘wp-json’ is default to initially secure the api call.
    • Get different reponse for list posts and single post responses.
    • For posts list (multiple) call, you can control ‘one_call’ fields from back end.
    • WordPress Posts fields filtering options from back end for posts list (loop of Posts).

    Hope this plugin help others like me who has plan to develop mobile application for WordPress Website using Ionic, Phonegap,React Native, Framework& and NativeScript etc.