I am currently using the JSON REST API (WP API) plug-in to get my post and page data.
I have noticed that none of my custom field data is returned in the json, and looking at the routes, i don’t think i can obtain these.
Any ideas via the current plug-in, or how I can accomplish this otherwise?
If you are using ‘advanced custom fields’ – until something more official is decided, you can use this plugin: https://github.com/times/acf-to-wp-api (and now on the shelf in standard wp plugin area too.)
It will include the custom fields under
acf: [],
in your json structure.To grab a custom field value using native WP functions only, add the following to your functions.php
Replace
'my_custom_field_key'
with your custom field key name.For multiple fields:
You need to create this file contain following code in
then you need to add
to your main.php in
to do that you need to require this file to main.php
You can manipulate the response and add custom fields to the JSON. I’m using Advanced Custom Fields in my example but you can just add any key/value-pairs to the data object before returning it.