Is there a way to get protected meta fields through any of the available built-in WordPress APIs? (xmlrpc, wp-json)

To me, the simplest solution would be creating additional field in JSON response and populating it with selected post meta:

function create_api_posts_meta_field() {

    // "tribe_venue" is your post type name, 
    // "protected-fields" is a name for new JSON field

    register_rest_field( 'tribe_venue', 'protected-fields', [
        'get_callback' => 'get_post_meta_for_api',
        'schema' => null,
    ] );
}

add_action( 'rest_api_init', 'create_api_posts_meta_field' );

/**
 * Callback function to populate our JSON field
 */
function get_post_meta_for_api( $object ) {

    $meta = get_post_meta( $object['id'] );

    return [
        'venue'   => $meta['_VenueVenue']   ?: '',
        'address' => $meta['_VenueAddress'] ?: '',
        'city'    => $meta['_VenueCity']    ?: '',
    ];
}

You should be able to see your meta data both in
/wp-json/wp/v2/tribe_venue/ and
/wp-json/wp/v2/tribe_venue/{POST_ID}

Leave a Comment