If you import all post types along with post metadata, you’ll get all the attachment references with it. The metadata will point to the media files relative to the /uploads/ directory (or wherever they currently are). As long as you maintain the same relative directory structure for the local versions, nothing needs to be altered, all will be maintained.
To maintain featured image relationships, the existing attachment IDs must be maintained. Which image file is a post’s featured image is managed by attachment ID. If you were to re-upload images into new attachment records, you’ll lose every post’s featured image relationship.
Huh. That’s not what I’m seeing at all. I exported all content from my site. When I imported it into my local wordpress, I did not select the “Download and import file attachments” checkbox because I didn’t want to have to download them all again. When it finished, none of my posts have featured images.
How do I “import all post types along with post metadata”? Maybe I did something wrong?
If all the images are in wp-content/uploads on the localhost site, use the plugin “better search replace”. Search for your domain (https://example.com) and replace with ” http://localhost “.
Thanks. That would work for inline images but my big concern is the featured image, and the import seems to have lost that entirely…unless you re-download and import file attachments.
What I’m really trying to do is figure out a way to get my existing posts/comments/attachment references into a clean, new database. Looks like out of the box export/import is not going to do what I need to do.
-
This reply was modified 9 months, 2 weeks ago by
glonojake.
A completely valid export/import should maintain featured image references. Aside from attachment IDs needing to be maintained, the key data that relates the attachment to a post is post meta keyed under _thumbnail_id, where the attachment’s ID is kept as the featured image for the post. Apparently something regarding this has not been maintained. Either the attachment IDs were changed or the post meta was not transferred or was corrupted somehow.
It sounds to me like your migration tool is somehow flawed. You could try manually exporting/importing just the _thumbnail_id post meta records. This can be done via the phpMyAdmin app. You should make a backup of the new DB before importing more data just in case something goes wrong. This is assuming the attachment IDs have been properly maintained in the posts table.
Alternately, manually export/import the entire DB, or at least the posts and postmeta tables. If you do this either start with an empty DB or empty posts and postmeta tables respectively.
There’s a separate issue with inline images that you may or may not be aware of. While attachment data should remain valid, the actual HTML of inline images in post content may need to be updated to reflect the local domain name (often localhost, YMMV). Some migration tools will manage this. Manual migration would require you to do this yourself. You can do a global search/replace operation with the Better Search and Replace plugin.
This flaw may not be noticeable if the original production site is still online. Unmodified HTML will pull images from the production site instead of your local copy. Everything will appear to be working, though not entirely correctly. If you’ll eventually migrate the local site back to production, it may not be worth changing the local HTML since you’d eventually need to change it back again.