support for featured images for custom post types wp-api

403 Views Asked by At

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;
        }

I got it!

the filter should look like

add_filter( 'rest_prepare_my_custom_post_type_slug', [ $this, 'add_featured_image' ], 10, 2 );
0

There are 0 best solutions below