Hi, i been trying make a theme change for my WordPress site.
So i have about 900 posts with :
Name: large-image Value: External url
I tried using the function explained on this site:
function get_image_id($image_url) {
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare(“SELECT ID FROM $wpdb->posts WHERE guid=’%s’;”, $image_url ));
return $attachment[0];
}
//Set the featured image on save of the post
add_action(‘save_post’, ‘set_featured_image_from_image’);
//set featured image as main image.
function set_featured_image_from_image()
{
$has_thumbnail = get_the_post_thumbnail($post->ID);
//If there is not featured image get the other image.
if (!$has_thumbnail) {
//get the url of the ACF image and get the ID of that image
$image_url = get_field(‘large_image’);
$image_id = get_image_id($image_url);
if ($image_id) {
set_post_thumbnail($post->ID, $image_id);
}
}
}
and _thumbnail_id.
But it’s not working for me, all the images are hosted elsewhere my media library is empty.
not sure what im doing wrong.
thanks in advance