• Resolved mayanktaker

    (@mayanktaker)


    I am creating custom endpoints for my Pods 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 ?
    the ad_1 field you see in link is come from relationship field of another custom post type to this custom post type. I want to show ads on my all posts of events custom post type so I created Advertisement custom post type and named it advts and I am using a relationship field in events custom post type to get advertisements from advts custom post type, so I can easily change all the ads between posts at once by changing the advt post type post.
    I can get what I want in default rest api call for events post type. But I am not getting image url on my custom rest api endpoint..
    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']['guid'];
    
        $i++;
    }
    
    return $data;
    
    }
    
    add_action('rest_api_init', function() { register_rest_route('myrestapi/v1', 'digievent', [ 'methods' => 'GET', 'callback' => 'digital_digievent', ]);
    
    });

    Need help in this line ??
    $data[$i]['top_image'] = $post->ad_1[0]['top_image']['guid'];

    Also check this link in which im working where im using this code :
    $data[$i]['top_image'] = $post->ad_1;
    you can see I am getting fields of ad_1.

    What I want is the image url(guid) of both top_image and bottom_image field for ad_1. and why I am creating custom rest api point is for reducing the loading of unnecessary fields.

Viewing 1 replies (of 1 total)
  • Plugin Contributor Jim True

    (@jimtrue)

    We are not the support process for the WP REST API as they have a really large group that provides support for Custom endpoints, etc. We really just enable the REST API and let you run with it.

    If you’ve ran into issues with our handling of the REST API, please report them through our bug process.

    Please file a bug report at: https://github.com/pods-framework/pods/issues/new. Include any relevant screenshots, your process of how you got the error/problem and any relevant code. Thanks!

Viewing 1 replies (of 1 total)
  • The topic ‘Pods Image field in REST aPI’ is closed to new replies.