0

I am facing a wired issue where the featured image is not getting updated properly if the id of the attachment was updated during import Here is the scenario.

Assume that you have exported content from an old site. A post (assume the id is 1000) has a featured image (attachment id is 1001).

Now we are trying to import it into a new site. Assume that in the new site the wp_posts table has ids till 1500.

When the import imports the attachment, the new id of the attachment will be 1501. The new id of the post will be 1502.

But in the post with id 1502, the post meta key _thumbnail_id is still set to 1001 and is not updated to 1501.

Because of this, the featured image doesn't work for the newly imported post. Is there a way to handle this id clash?

0

I ended up fixing this by preventing the id clash by adding a huge number to the post id column and then exporting the content.

The queries I ran are

; Update the post ids of all attachments. I choose 1000,000 since I already had more than 100,000 posts.
update wp_posts set id = id + 1000000 where post_type = 'attachment';

; Update the postmeta fields of all attachments
update wp_postmeta set post_id = post_id + 1000000 where meta_key = '_wp_attachment_metadata';
update wp_postmeta set post_id = post_id + 1000000 where meta_key = '_wp_attachment_image_alt';
update wp_postmeta set post_id = post_id + 1000000 where meta_key = '_wp_attached_file';


; Update the featured image ids
update wp_postmeta set meta_value = meta_value + 1000000 where meta_key = '_thumbnail_id'

; You needs this if you are using featured images for terms. Change the meta key to whatever you are using.
update wp_termmeta set meta_value = meta_value + 1000000 where meta_key = 'featured_image_id' and meta_value > 0;

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.