I’m using the Jetpack for WordPress to produce a JSON API for my Events custom post type, which works fine:
https://public-api.wordpress.com/rest/v1/sites/MYSITE/posts/?type=events&number=100
However, this CPT also includes quite a few Post Meta Fields generated by Custom Meta Boxes and Fields for WordPress. These fields include things like:
_ecmb_supporting_bands
_ecmb_tickets_avail
_ecmb_event_agelim
And by default these fields aren’t returned by the JSON API… I’ve tried to run queries like:
https://public-api.wordpress.com/rest/v1/sites/MYSITE/posts/?type=events&number=100&meta_key='_ecmb_supporting_bands'
Or without quotes around the meta key:
https://public-api.wordpress.com/rest/v1/sites/MYSITE/posts/?type=events&number=100&meta_key=_ecmb_supporting_bands
Unfortunately this doesn’t work. Does anyone know how I can return these custom meta fields in my JSON response?
From the documentation:
According to the JetPack JSON API docs:
so the
rest_api_allowed_public_metadata
filter is what you are looking for.From the source code:
If you check the JetPack’s source code, you will find this part:
in the file
class.json-api-endpoints.php
.You can also check out the
allow_bbpress_public_metadata()
function here to see how to implement thisrest_api_allowed_public_metadata
filter.Example:
Here is a similar example for your case:
with the JSON output similar to this one:
In addition to @birgire reply, placing the filter code somewhere can be tricky:
This is why what I did was to use the following plugin that allows you to write your custom actions and filters within WordPress admin panel without having to worry about losing it. http://wordpress.org/plugins/add-actions-and-filters/ .
I had the same problem than you and after including the filter now it works like a charm.