Function to replace the url of featured images in the wordpress database?

by Marc   Last Updated July 11, 2019 20:08 PM

Sometime ago I uploaded all images of my wordpress installation to the /media/ folder. So urls where something like this:

http://example.com/media/featured-image-2010.jpg

This folder is very, very big. So I switched to a date folder structure:

http://example.com/media/2019/07/featured-image-2019.jpg

So I wrote a plugin to loop through all images to get the url. Here is a excerpt:

foreach ($posts as $post):
  $featured_image_url = wp_get_attachment_url(get_post_thumbnail_id( $post->ID ));
  echo featured_image_url;
endforeach;

Nearly half of my images have still the flat media-folder structure. But I want to delete all images in the root of the media folder and sort them with date folders.

So my idea is to copy the files to the corresponding folder of the post and change the url of the featured image attachment. But there is no wordpress way to change urls with a function. Is there a trick to it? I looked in the database and I don't really understand how the connection between a featured image and a posts works.

How can I change the url of a featured image?



Related Questions


Updated June 03, 2017 15:08 PM

Updated October 06, 2017 22:08 PM

Updated May 14, 2015 06:03 AM

Updated May 24, 2015 01:03 AM