Prevent duplicate postmeta records insertion
Issue
- On import of already existing attachment/post/page, the importer gives error "Page/Post/Attachment already exists." but imports the postmeta records from source.
Fix
- Replace the function to add postmeta only if it's not exist
Hi @sagarsdeshmukh, I apologise for how long this PR has sat here without any comment.
Does swapping from add_post_meta() to update_post_meta() break the expectation that WordPress can have multiple post meta records per name? For example, you can have name: animal value: cat and name: animal value: dog attached to the same post.
It might be better to explicitly only import postmeta/taxonomies when the post itself is imported, rather than relying upon the behaviour of update vs add.
Part of the problem here is that there can actually be multiple identical post meta records per name (maybe for counting thumbs-up/thumbs-down as an example use case):
wp> add_post_meta( 5, 'foo', 'bar' );
=> int(200)
wp> add_post_meta( 5, 'foo', 'bar' );
=> int(201)
wp> get_post_meta( 5, 'foo' );
=> array(2) {
[0]=>
string(3) "bar"
[1]=>
string(3) "bar"
}
If the import is run multiple times, using add_post_meta will result in multiple instances of each post meta on a post because these are imported regardless of whether the post previously existed, and independent of how many times they already exist on a post.
Should there be a check of existence of the key / value pair that is being imported, and if exists, do nothing? @sagarsdeshmukh @jmdodd that might solve the issue here?
This PR is being here for long period of time, I would like to ask the core team of this project, do you expect any action/input/changes from my end, please let me know? cc: @ocean90 @jffng @boonebgorges @dd32