I am creating custom endpoints for my Custom Post Type to remove the unnecessary fields and make request faster. I want to fetch image url from my custom post type. Please check the code for more info. I cant get the Image field. In this link, this is what I am getting on default rest api call which is what I want on my custom endpoint**(image url of top_image)**. Please check the method im using and getting null data. Where I am doing wrong ? Please help me.
Here is my Rest api code :
function digital_digievent() {
$args = [
'numberposts' => 99999,
'post_type' => 'digievent'
];
$posts = get_posts($args);
$data = [];
$i = 0;
foreach($posts as $post) {
$data[$i]['id'] = $post->ID;
$data[$i]['EventTitle'] = $post->post_title;
$data[$i]['EventOrganizeBy'] = $post->organize_by;
$data[$i]['EventPlace'] = $post->event_place;
$data[$i]['EventDate'] = $post->event_date;
$data[$i]['EventTime'] = $post->event_time;
$data[$i]['EventContent'] = apply_filters( 'the_content', $post->post_content );
$data[$i]['EventExcerpt'] = $post->post_excerpt;
$data[$i]['Slug'] = $post->post_name;
$data[$i]['EventLink'] = get_the_permalink($post->ID);
$data[$i]['EventDate'] = $post->post_date;
$data[$i]['EventGuid'] = $post->guid;
$data[$i]['EventFeaturedImage']['thumbnail'] = get_the_post_thumbnail_url($post->ID, 'thumbnail');
$data[$i]['EventFeaturedImage']['medium'] = get_the_post_thumbnail_url($post->ID, 'medium');
$data[$i]['EventFeaturedImage']['large'] = get_the_post_thumbnail_url($post->ID, 'large');
$data[$i]['top_image'] = $post->ad_1[0]['top_image'];
$i++;
}
return $data;
}
add_action('rest_api_init', function() { register_rest_route('myrestapi/v1', 'digievent', [ 'methods' => 'GET', 'callback' => 'digital_digievent', ]);
});
What I want is the image url(guid) of both top_image and bottom_image field for ad_1.