Support

Account

Home Forums Backend Issues (wp-admin) "Featured image" still intact upon removal

Solved

"Featured image" still intact upon removal

  • @jonathan provided a nifty bit of code over at THIS thread which answered the following question:

    Is it possible to set an image (uploaded with ACF) automatically as the featured image of a post?

    It does the job perfectly. So thanks, Jonathan.

    However, it has come to my attention that if the image is REMOVED from the custom field, the featured image of the post remains intact. I’d like to ensure that if the user removes the image from the custom field, the featured image data assigned to the post is also cleared (IE. the post no longer has a featured image).

    For reference, here is the code from the other thread:

    <?php
     
    function acf_set_featured_image( $value, $post_id, $field  ){
        
        if($value != ''){
    	    //Add the value which is the image ID to the _thumbnail_id meta data for the current post
    	    add_post_meta($post_id, '_thumbnail_id', $value);
        }
     
        return $value;
    }
    
    // acf/update_value/name={$field_name} - filter for a specific field based on it's name
    add_filter('acf/update_value/name=cursusfoto', 'acf_set_featured_image', 10, 3);
     
    ?>

    I started a new topic because A) the previous thread is a few years old, and B) the previous thread ended up going in a totally different direction.

    Many thanks

  • 
    <?php
     
    function acf_set_featured_image( $value, $post_id, $field  ){
        
        if($value != ''){
          //Add the value which is the image ID to the _thumbnail_id meta data for the current post
          add_post_meta($post_id, '_thumbnail_id', $value);
        } else {
          delete_post_meta($post_id,'_thumbnail_id', $value);
        }
     
        return $value;
    }
    
    // acf/update_value/name={$field_name} - filter for a specific field based on it's name
    add_filter('acf/update_value/name=cursusfoto', 'acf_set_featured_image', 10, 3);
     
    ?>
    
  • Thanks John,

    In my attempts I was trying to use add_post_meta again. I completely looked over delete_post_meta.

    Thanks for putting me straight 🙂

  • Hi @hube2, thanks again for your support with my previous question. I have another one that’s related.

    The above code works for both back-end post creation and also front-end post creation (by public users).

    However, I noticed that when looking in the media browser the images, although assigned as thumbnails, are not “attached” to any particular posts.

    Is there a way the image can be “attached” to the post too when submitted from the front-end?

    I have a plugin that deletes all associated post content upon post deletion (all attached media), which is why I’d like to get this working too.

    Here’s the code that created the post from the front end:

    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    function my_pre_save_post( $post_id ) {
    
        // Create a new application post
        $post = array(
    		'post_status' => 'publish',
    		'post_type'   => 'application',
        );
    
        // insert the post
        $post_id = wp_insert_post( $post );
        
        // return the new ID
        return $post_id;
    
    };

    Many thanks.

    Jack

  • You need to update the post parent of the image to attach it to the post, you could to that in the same function that you’re using to set the featured image.

    
    <?php
     
    function acf_set_featured_image($value, $post_id, $field ){
      if ($value != '') {
        //Add the value which is the image ID to the _thumbnail_id meta data for the current post
        add_post_meta($post_id, '_thumbnail_id', $value);
        // attach image to post
        wp_update_post(
          array(
            'ID' => $value, 
            'post_parent' => $post_id
          )
        );
      } else {
        delete_post_meta($post_id,'_thumbnail_id', $value);
      }
      return $value;
    }
    // acf/update_value/name={$field_name} - filter for a specific field based on it's name
    add_filter('acf/update_value/name=cursusfoto', 'acf_set_featured_image', 10, 3);
     
    ?>
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘"Featured image" still intact upon removal’ is closed to new replies.