Adding Featured Image Programatically

enter image description here

I have thoroughly read the documentation
my function is

function set_featured_image($image, $post_id){
    $image_path = '/var/www/html/wp-content/plugins/apkplugin/images/';
    $wp_filetype = wp_check_filetype($image_path.$image, null );
    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => sanitize_file_name($image),
        'post_content' => '',
        'post_status' => 'inherit'
    $attach_id = wp_insert_attachment( $attachment, $image_path.$image, $post_id );
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
    $res1= wp_update_attachment_metadata( $attach_id, $attach_data );
    $res2= set_post_thumbnail( $post_id, $attach_id );

I checked the database and the images are there but my post does not have an image attached and when i open media gallery
I see these default thumbnails

what am i doing wrong?

Read more here:: Adding Featured Image Programatically

Leave a Reply

Your email address will not be published. Required fields are marked *