0

I have a website that I am updating, it has over 400 posts so updating these manually isn't a very good option. The theme that was used is no longer supported, it has a custom field setup for the featured image. I want to convert that featured image to the WordPress featured image. How can I do this? In the database, the url for the images are stored under wp_postmeta and the meta_key is lifestyle_post_image.

0

Migrating from one to another isn't that complicated usually. As long as you have the logic you can write code to make it happen and run in bulk or tie it to every time featured image is requested for gradual change over.

The bigger issue I see there is that you say images are stored as URLs. WordPress generally manipulates attachment IDs for this, including featured images. URLs are just too inconvenient to work with for many related purposes.

While going from ID to URL is trivial, going from URL to ID is huge pain in general case. Arbitrary URLs might not even be local attachments. You could see my answer on going from URL to ID for a starting point.

Overall without knowing more of your context it is hard to advise on specific approach. If getting to native UI is priority then bulk thorough migration is probably in order. If older posts just need to work sometimes it is easier to just leave them alone with legacy code and move on to new workflow for the current stuff.

0

I ended up finding another post that helped me achieve what I was trying to do. https://stackoverflow.com/questions/4371929/convert-custom-field-image-to-become-featured-image-in-wordpress

$uploads = wp_upload_dir();

// Get all attachment IDs and filenames
$results = $wpdb->get_results("SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file'");

// Create an 'index' of attachment IDs and their filenames
$attachments = array();
foreach ($results as $row)
$attachments[ intval($row->post_id) ] = $row->meta_value;

// Get all featured images
$images = $wpdb->get_results("SELECT post_id, meta_value AS 'url' FROM $wpdb->postmeta WHERE meta_key = 'lifestyle_post_image'");

// Loop over each image and try and find attachment post
foreach ($images as $image) {
if (preg_match('#^https?://#', $image->url))
    $image->url = str_replace($uploads['baseurl'], '', $image->url); // get relative URL if absolute

$filename = ltrim($image->url, '/');

if ($attachment_ID = array_search($filename, $attachments)) {
    // found attachment, set post thumbnail and delete featured image
    update_post_meta($image->post_id, '_thumbnail_id', $attachment_ID);
    delete_post_meta($image->post_ID, 'Featured Image');
    }
}
-1

https://wordpress.org/plugins/quick-featured-images/ may very well be what you are looking for. It recently helped me greatly in a similar batch processing situation, and has a bunch of other useful features. Simply saves coding something custom!

  • If recommending plugin please try to make answer specific to the question asked. How does it help to go from stored URL to a featured image? – Rarst Dec 20 '16 at 0:13
  • Via a UI-based bulk process as I indicated, Incidentally your answer is a comment not an answer, and takes a fair few words more to suggest a bulk process, and then does not recommend a plugin or specify code to carry it out. – mayersdesign Dec 20 '16 at 10:15
  • I do not see capability to to migrate from arbitrary URL in custom field to featured image in description of that plugin. – Rarst Dec 20 '16 at 10:18
  • I am not seeing how this plugin will help me, it allows me to change the featured image but does not allow me to pull from another field. – Chris Wathen Dec 20 '16 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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