How do I add support for featured images ins case of custom post types in wp-api?

I found the following snippet which is enabling this kind of support for wordpress normal post but nothing about custom post types

function init() {
            add_filter( 'rest_prepare_post', [ $this, 'add_featured_image' ], 10, 2 );

        }
        function add_featured_image( $data, $post ) {
            $sizes        = [ 'thumbnail' => '', 'medium' => '', 'large' => '', 'full' => '' ];
            $_data        = $data->data;
            $thumbnail_id = get_post_thumbnail_id( $post->ID );
            foreach ( $sizes as $size => $src ) {
                $sizes[ $size ] = wp_get_attachment_image_src( $thumbnail_id, $size )[0];
            }
            $_data['featured_image_url'] = $sizes;
            $data->data                  = $_data;
            return $data;
        }

Read more here: support for featured images for custom post types wp-api


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: