wordpress-importer icon indicating copy to clipboard operation
wordpress-importer copied to clipboard

Prevent duplicate postmeta records insertion

Open sagarsdeshmukh opened this issue 5 years ago • 4 comments

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

sagarsdeshmukh avatar May 15 '20 12:05 sagarsdeshmukh

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.

dd32 avatar Oct 12 '20 01:10 dd32

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.

jmdodd avatar Dec 08 '20 19:12 jmdodd

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?

stevus avatar Aug 18 '21 19:08 stevus

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

sagarsdeshmukh avatar Dec 20 '23 06:12 sagarsdeshmukh