Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Re-Attach unattached media to the correct Post Id. WordPress


I have two requests. They are urgent requests and I would need it before Monday morning.:

<strong>Request 1</strong>
I use a plugin that automatically downloads remote images and save them.
However, after a few months of work I noticed all the images in Media > Library are "unattached"

These images are actually in posts and working.
What I need is a plugin/function that scans all the posts. Find the images in the posts, and attach it to the post.

It is very important that it keeps the exact same path and file name.


I used this very usefull plugin: [[LINK href=""]][[/LINK]]

It basically scans posts for external images and re-attach them. NOT something I want. But perhaps you can modify the code somehow to fit what I need. Because this plugin already scans posts via dates, and has a good UI already built in.

I run the normal plugin, and I get this image:

[[LINK href="

This is a good sign because it scans the correct Post ID, and it does pick up the correct images and amount of images per post. So not all hope is lost.

Then I tried this:

If you remove the lines at 525:

if ( stripos( $img, $path ) !== false ) {
$response .= 'Img already in media library<br>';

<strong>It seems to reattach all the files to the correct posts!</strong>

BUT it also <strong>re-downloads </strong>them, so there are <strong>duplicates</strong>. And most importantly, it <strong>renames </strong>them, which should <strong>not </strong>happen. The file name should stay the same as the original. So it should only attach it, not download and rename.

So you can either modify the above plugin. Or just make your own scan and attach function. I'm happy with either.

<strong>Request 2</strong>

Just to take a look at the image auto save function. To make sure it attach images correctly to post. So this won't happen again.


<strong>UPDATE</strong>: This is the full code of the plugin I mentioned above. If anyone want to take a shot

[[LINK href=""]][[/LINK]]

I want to disable to renaming and re downloading of the images. Just doing the re-attachment part is needed.

Answers (1)


Arnav Joy answers:

can you provide me access to your site to look please.

my email is : [email protected]