Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted
+50

From the documentation:

According to the JetPack JSON API docs:

By default, all metadata keys are allowed in the API, as long as they are not protected keys. Any metadata key that starts with _ is by default protected. Protected metadata keys can, however, be accessed and edited by users with the edit_post_meta (used for editing and viewing), add_post_meta and delete_post_meta capabilities as appropriate for each operation. We’ve also added a filter rest_api_allowed_public_metadata that allows you to specifically whitelist certain metadata keys to be accessed by any user, even if that key is protected.

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:

    function is_metadata_public( $key ) {
            if ( empty( $key ) )
                    return false;

            // whitelist of post types that can be accessed
            if ( in_array( $key, apply_filters( 'rest_api_allowed_public_metadata', array() ) ) )
                    return true;

            return false;
    }

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 this rest_api_allowed_public_metadata filter.

Example:

Here is a similar example for your case:

/**
 * Whitelist protected meta keys
 *
 * @param array $allowed_meta_keys 
 * @return array $allowed_meta_keys 
 */
function custom_rest_api_allowed_public_metadata( $allowed_meta_keys )
{
    // only run for REST API requests
    if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST )
        return $allowed_meta_keys;

    $allowed_meta_keys[] = '_ecmb_supporting_bands';
    $allowed_meta_keys[] = '_ecmb_tickets_avail';
    $allowed_meta_keys[] = '_ecmb_event_agelim';

    return $allowed_meta_keys;
}

add_filter( 'rest_api_allowed_public_metadata', 'custom_rest_api_allowed_public_metadata' );

with the JSON output similar to this one:

"metadata":[{"id":"196711","key":"_ecmb_event_agelim","value":"18"},
            {"id":"196709","key":"_ecmb_supporting_bands","value":"The Rolling Stones"}, 
            {"id":"196710","key":"_ecmb_tickets_avail","value":"5500"}]
share|improve this answer

In addition to @birgire reply, placing the filter code somewhere can be tricky:

  • If you place it under your theme's "functions.php" file, a theme update could overwrite your code
  • Writing a plugin for just adding a filter is a lot of fuzz

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.