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'];


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.

1 Answer 1


$post->ad_1[0]['top_image'] will not return you data, because you are not in JS which got response from your REST api which had injected ad_1 value into object which it returns to you.

In your PHP code $post is of type WP_Post and it doesn't have ad_1 in it, that's why you getting nulls. I don't know what ad_1 is in your data structure. Maybe it's ACF field or just metafield, if it's ACF you need to access it like get_field( 'ad_1', $post ).

Check your code which injects ad_1 value into your current REST route which responds with single item, and do the same for bulk.

  • Yes ad_1 is custom post type field which I am getting from advertisement custom post type to this events custom post type with the help of relationship field. To be more specific, advertisement custom post type has 2 top and bottom image field which is file upload field. I want 2 images between the content of events post dynamically because i will have to change all image ads at once later when i want to. And its not ACF, its coming form Pods framework which is custom post type builder just like ACF. :-) Mar 18, 2020 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.