0

I am trying to add the featured image to posts and that works, however, the featured image is apparently set everytime I check my post.

<?php 

$post_id = get_queried_object_id();

$bla = "myImageURL";

// Set the image URL
$image_url = $bla;

// Download the image
$image_id = media_sideload_image($image_url, $post_id, '', 'id');

// Check for errors
if (is_wp_error($image_id)) {
    print_r($image_url);
} else {
    // The image was downloaded successfully. Now set it as the featured image for the post
    set_post_thumbnail($post_id, $image_id);
}

?>

So, every time I view my post, it adds the featured image AGAIN to my media library.

How can I prevent that from happening?

I tried:

elseif (!has_post_thumbnail($post_id)) {
    set_post_thumbnail($post_id, $image_id);
}

But sadly this did not work either. The below also did not work (based on answer here below):

<?php 

$post_id = get_queried_object_id();

$bla = MyIMGURL;

// Set the image URL
$image_url = $bla;

'attachment', 'meta_key' => '_wp_attached_file', 'meta_value' => sanitize_file_name(basename($image_url)); if (!$existing_image) { // Image does not exist in the media library, so download it 
$image_id = media_sideload_image($image_url, $post_id, '', 'id'); // Check for errors 
if (is_wp_error($image_id)) { print_r($image_url); } else { // The image was downloaded successfully.


?>
6
  • Where is your first block of code? Jan 9 at 3:41
  • First block of code? This is all the code I have.
    – Johan
    Jan 9 at 8:58
  • But where is it? In a function or template? Or just straight in functions.php or a plugin as-is? Jan 9 at 10:04
  • Ah. In the single.php file.
    – Johan
    Jan 9 at 10:06
  • Well single.php is definitely not the right place to handle uploading an image and setting it as the featured image. Templates are for displaying posts, not handling logic like that. The template code runs every time the post is viewed which is why multiple copies of the image are being created. What are you ultimately trying to achieve? Setting the featured image is already possible through the editor, so what's this code for? Jan 9 at 13:17

1 Answer 1

0
<?php 

$post_id = get_queried_object_id();

$bla = "myImageURL";

// Set the image URL
$image_url = $bla;

// Check if the image already exists in the media library
$existing_image = get_posts(array(
    'post_type' => 'attachment',
    'meta_key' => '_wp_attached_file',
    'meta_value' => sanitize_file_name(basename($image_url))
));

if (!$existing_image) {
    // Image does not exist in the media library, so download it
    $image_id = media_sideload_image($image_url, $post_id, '', 'id');

    // Check for errors
    if (is_wp_error($image_id)) {
        print_r($image_url);
    } else {
        // The image was downloaded successfully. Now set it as the featured image for the post
        set_post_thumbnail($post_id, $image_id);
    }
} else {
    // Image already exists in the media library, so set it as the featured image for the post
    set_post_thumbnail($post_id, $existing_image[0]->ID);
}

?>
2
  • Sorry, I can't really read what you wrote. I am editing my post to show you what I think you mentioned (getting an error btw).
    – Johan
    Jan 9 at 9:02
  • Parse error: syntax error, unexpected ','
    – Johan
    Jan 9 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.