php – Add Featured Image to Existing Post

Question

I have already created thousands of product as posts (CSV Import). They have a custom post type. Now, I would like to assign featured image to each existing post programmatically (without manually entering each post and setting the featured images more than 3,000 times).

Additional info:

  • Import plugin have not worked out, so I cannot attach the images using CSV import.
  • I use a custom field called ‘ean’ with each prduct, and it is standing as the unique serial number. It is an interger, for example: “5999626322456”. The same interger is standing for name of the image (i.e. 5999626322456.jpg).

What code do you recommend to use? I tried these two (among many), but they do not work. What do I do wrong?

function set_post_thumbnail( $post, $thumbnail_id ) {
    $post    = get_post( $post );
    $ean_url = rwmb_get_value( 'ean' );
    $kep_url = $ean_url . '.jpg';
    $thumbnail_id = attachment_url_to_postid( $kep_url );
    if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
        if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) {
            return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
        } else {
            return delete_post_meta( $post->ID, '_thumbnail_id' );
        }
    }
    return false;
}

// Add Featured Image to Post

$ean_url          = rwmb_get_value( 'ean' );
$image_url="/wp-content/uploads/" . $ean_url . '.jpg'; // Define the image URL here
$image_name       = $ean_url . '.jpg';
$upload_dir       = wp_upload_dir(); // Set upload folder
$image_data       = file_get_contents($image_url); // Get image data
$unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
$filename         = basename( $unique_file_name ); // Create image file name

// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
    $file = $upload_dir['path'] . "https://wordpress.stackexchange.com/" . $filename;
} else {
    $file = $upload_dir['basedir'] . "https://wordpress.stackexchange.com/" . $filename;
}

// Create the image  file on the server
file_put_contents( $file, $image_data );

// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );

// Set attachment data
$attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title'     => sanitize_file_name( $filename ),
    'post_content'   => '',
    'post_status'    => 'inherit'
);

// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );

// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');

// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );

// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );

// And finally assign featured image to post
set_post_thumbnail( $post_id, $attach_id );

0
Jani 2 months 2021-07-25T12:45:54-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse