4

I have many duplicated wp_postmeta record (about hundreds record with same post_id & meta_key) and need to delete safely via mysql, is there any effective way to delete the postmeta record.

wp_postmeta table

Further question is how to create code implementation to prevent this postmeta duplication. Many Thanks :-)

2 Answers 2

4

It is quite complicates grouped request, so to avoid problems, consider to use Cleanup Duplicate Meta plugin.

The reason why duplicate entries are occured is usage of add_post_meta(). Just use update_post_meta() (even to create meta, it works) - and you will have always one meta field with certain name per post.

1
  • That plugin saved me hours of work thanks! still working today Mar 28, 2020 at 18:09
4

Try it to delete duplicated wp_postmeta record:

DELETE FROM `wp_postmeta` WHERE `meta_id` NOT IN (SELECT * FROM (SELECT MAX(`pm`.`meta_id`) FROM `wp_postmeta` pm GROUP BY `pm`.`post_id`, `pm`.`meta_key`) x)

Use update_post_meta instead of add_post_meta to prevent create duplicate postmeta.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.