WordPress has several APIs build in:
by default they don't return custom post types or protected meta keys.
I want to access the protected meta fields of a plugin. I tried to follow the examples given here and here.
I did manage that the wp-json API would return custom post types by adding this code:
/**
* Add REST API support to an already registered post type.
*/
add_action( 'init', 'my_custom_post_type_rest_support', 25 );
function my_custom_post_type_rest_support() {
global $wp_post_types;
// be sure to set this to the name of your post type!
$post_type_name = 'tribe_venue';
if( isset( $wp_post_types[ $post_type_name ] ) ) {
$wp_post_types[$post_type_name]->show_in_rest = true;
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
}
$post_type_name2 = 'tribe_events';
if( isset( $wp_post_types[ $post_type_name2 ] ) ) {
$wp_post_types[$post_type_name2]->show_in_rest = true;
$wp_post_types[$post_type_name2]->rest_base = $post_type_name2;
$wp_post_types[$post_type_name2]->rest_controller_class = 'WP_REST_Posts_Controller';
}
}
But I was not able to include protected meta keys in the response.
I tried the following code:
add_filter( 'is_protected_meta', function ( $protected, $key, $type ) {
if ( $type === 'tribe_venue' && $key === '_VenueVenue' ) {
return true;
}
return $protected;
}, 10, 3 );
add_filter( 'rae_include_protected_meta', '__return_true' );
and the following code:
function custom_rest_api_allowed_public_metadata($allowed_meta_keys){
$allowed_meta_keys[] = '_VenueVenue';
$allowed_meta_keys[] = '_VenueAddress';
$allowed_meta_keys[] = '_VenueCity';
return $allowed_meta_keys;
}
add_filter( 'rest_api_allowed_public_metadata', 'custom_rest_api_allowed_public_metadata' );
but neither works.
Does anybody know what is needed so make such protected fields accessible through any of the APIs? Is there a working example anywhere?