2 comments

  1. 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"}]
    
  2. 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.

Comments are closed.