Reinstall WordPress while keeping attachments

I have a WordPress installation that I want to reinstall. However, I also have in it several uploaded files (.doc and PDFs) that are linked by external sites. They are not used in any of my own pages: I just uploaded them to my WordPress site instead of putting them in a Dropbox or a Google Drive, and now I can’t (or shouldn’t) delete them.

The good thing: they are all on the same directory: /wp-content/uploads/2020/07/, so I could just delete everything, reinstall WordPress and then upload the files to the same directory… However, I don’t like the idea of having stray files in my WordPress installation without WP knowing about them, so I would also like to keep the DB entries that keep track of those files.

If I’m not mistaken, the attachments are kept in the wp_posts table, so what I would have to do is:

  1. Dump all the wp_posts entries referring to the attachments.
  2. Dump all the wp_postmeta entries referring to the entries in wp_posts.
  3. Inserting those entries by hand in the new WordPress’ tables.

Is there anything I’m missing? Any other place in the WordPress DB where there might be information?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Depending on how those inbound links work, this could be simpler than you think.

If the links are direct to files in the file system, WordPress isn’t actually needed at all – just a corresponding path for a valid URL.

So let’s say you install WP today, the ‘earliest’ folder in uploads will be 2020/09. If you setup WP as normal and simply copy in the 2020/07 folder from your previous install, the files will be accessible to external visitors as normal. WP will not overwrite that folder as the writing of files is based on upload date (by default). It can’t go back in time (without a hack).

The problem comes if their links are to attachment pages, rather than the attachments themselves. In that case, I think the simplest and most manageable option is actually to setup some redirects in htaccess. Then create new attachments in your new WP install and redirect old path to new.

Method 2

I agree with @t2pe. Moreover, I would consider moving these PDFs and DOCs to independent location. Just like you mentioned: a public folder in Dropbox or Google Drive would do the trick.

Then, create a directory 2020/07 in uploads and from there, in htaccess redirect all traffic to a cloud storage.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x