Hi -
I'm creating a custom REST endpoint and I'm having trouble populating Metabox Fields.
website
returns true/false and cities
is null.
Thanks for your help!
if( !class_exists( 'toAPI' ) ) {
class toAPI {
function __construct() {
add_action( 'rest_api_init', [$this, 'init'] );
}
function init() {
register_rest_route( 'to/v1', '/destinations', [
'methods' => 'GET',
'callback' => [$this, 'get_destinations'],
] );
}
// Get recent destinations
function get_destinations( $params ) {
$destinations = get_posts( [
'post_type' => 'destinations',
'posts_per_page' => 10
] );
foreach( $destinations as &$d ) {
$d->thumbnail = get_the_post_thumbnail_url( $d->ID );
$d->website = rwmb_meta( 'website' );
$d->cities = MB_Relationships_API::get_connected( [
'id' => 'destination-city',
'to' => $d->ID, // You can pass object ID or full object
] );
foreach ( $d->cities as &$c ) {
$c->post_title;
$c->id;
}
}
return $destinations;
}
}
new toAPI();
}