Why does Wordpress featured image not save when I publish my post?

I inherited this website from another developer. We recently updated the Advanced Custom Fields PRO plugin to fix a different issue and it was working fine. I got a call this morning that the featured image appears when being added to the post, but when they publish it the featured image disappears.

I've checked to make sure that featured images are allowed. Existing posts display the featured image, but editing or adding new posts and then publishing does not attach the image to the post.

I've looked at the save_post hook methods and they don't touch the featured image at all.

Any ideas?

Here's the method added via the 'save_post' hook:

function save_listing($listing_id, $post) {
  global $wpdb;
  if ('listings' != $post->post_type)
    return;
  //store fullDescription, keywords and categories in wp_listings
  $full_description = get_field('listing_full_description', $listing_id);
  $keywords = get_field('listing_keywords', $listing_id);
  $terms = wp_get_post_terms($listing_id, 'listing-categories', ['fields' => 'names']);
  $categories = implode(", ", $terms);
  $tier = get_field('listing_tier', $listing_id);
  $tier = array_search($tier, ['standard', 'premium', 'platinum']) + 1;
  $wpdb->replace('wp_listings', ['id' => $listing_id, 'title' => $post->post_title, 'fullDescription' => $full_description, 'keywords' => $keywords, 'categories' => $categories, 'tier' => $tier], ['%d', '%s', '%s', '%s', '%s', '%d']);

  $locations = get_field('listing_locations', $listing_id);
  $wpdb->delete('wp_locations', ['listing_id' => $listing_id], ['%d']);
  if (!isset($locations))
    return;
  if ($post->post_status != 'publish')
    return;
  foreach ($locations as $loc_number => $location) {
    $disabled = isset($location['disable_map_location']) && $location['disable_map_location'] ? 1 : 0;

    $wpdb->insert('wp_locations', ['listing_id' => $listing_id, 'loc_number' => $loc_number, 'lat' => $location['map']['lat'], 'lng' => $location['map']['lng'],
      'zip' => $location['zip'], 'city' => $location['city'], 'state' => $location['state'], 'disabled' => $disabled], ['%d', '%d', '%f', '%f', '%s', '%s', '%s', '%d']);
  }
}

add_action('save_post', 'save_listing', 100000000, 2);

Also verified that featured images are supported

if (function_exists('add_theme_support')) {  
add_theme_support('post-thumbnails'); }

I have verified that on post save the post meta data includes "_thumbnail_id" and there is a post attachment corresponding with that ID. However when the page reloads there is no Featured Image.

Solutions

So after looking at the $_POST after saving and then cross-referencing the post meta data I found that the _thumbnail_id was not getting saved in the meta data. Still not sure why, but I fixed it by adding this code to my save_post hook method:

update_post_meta($post->ID, '_thumbnail_id', filter_input(INPUT_POST, '_thumbnail_id') );

This feels a bit hacky but the whole site is hacky :)

Similar questions

Create a new post using rest api and save featured image using an external image url
I am trying to create a post using the rest API. Which I am able to do with a fetch request as below. OurPostData contains the title, content and URL of a featured image which is an external url This works except for the featured image. If I am able to do pass it to the callback function done using the below code may be I can achieve that? But I do...
Getting the Featured image URL and inserting it as Custom Field on Post update / publish
I've tried a number of solutions posted here regarding converting custom fields to featured images - of course in reverse; but non seem to work for me. What I'm trying to do is grab the Featured Image URL once the image is uploaded and set as a featured image; then automatically set the full size image URL as a custom field. This needs to work on b...
Require a featured image to publish post
I have a multi author WordPress blog. I'd like to allow publishing of a post only when a featured image is set. How can that be done?
Get first URL from custom field, download and set as featured image on post publish
I have a plugin that is adding text and a URL to a custom field when the post is published (not saved as draft). I want to get/make a function or plugin to search within the custom field and sideload the image from a URL, then add that image as the featured image. Doing some searching I found this function from another StackExchange post that was d...
How to debug | Some times the "Save Draft" button seems to spin but does not actually save
I am using wordpress 3.5.1 and lately there seems to be a recurring problem with the save draft function where it seems like the draft was saved (i.e the wheel finishes spinning without an error message) but alas, all edits are lost. This happens both in the regular editor as well as in the distraction free wring space (where it is extremely frustr...
Dynamic Featured Image - WordPress - is not getting the first featured image
I've installed the latest Dynamic Featured Image 3.1.2 and when I try to just print out the array of featured images on the page I only get Featured Image 2 and onward. Even though I've added more than one Featured image it only shows from Featured Image 2 and on in the array. Did I miss something else that I need to do?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .